Guest User

ab string

a guest
Sep 16th, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. class TestClass
  4. {
  5.     public static void main(String args[])throws Exception
  6.     {
  7.         FastReader fr=new FastReader();
  8.         StringBuilder sb=new StringBuilder();
  9.         int t=fr.nextInt();
  10.         while(t-->0)
  11.         {
  12.             int n=fr.nextInt();
  13.             /*
  14.             6 states-
  15.             1.a
  16.             2.b
  17.             3.ab
  18.             4.ba
  19.             5.aba
  20.             6.bab
  21.              */
  22.             int i,j,ans=1,c[]=new int[6],p[]=new int[6];
  23.             int ch=fr.read();
  24.             p[0]=ch=='a'?1:0;
  25.             p[1]=ch=='b'?1:0;
  26.  
  27.             for(i=1;i<n;i++)
  28.             {
  29.                 ch=fr.read();
  30.                 for(j=0;j<6;j++) c[j]=p[j];
  31.                 if(ch=='a')
  32.                 {
  33.                     c[0]++;
  34.                     c[3]=Math.max(p[3]+1,p[1]+1);
  35.                     c[4]=Math.max(p[4]+1,p[2]+1);
  36.                 }
  37.                 else
  38.                 {
  39.                     c[1]++;
  40.                     c[2]=Math.max(p[0]+1,p[2]+1);
  41.                     c[5]=Math.max(p[3]+1,p[5]+1);
  42.                 }
  43.  
  44.                 for(j=0;j<6;j++)
  45.                 {
  46.                     ans=Math.max(ans,c[j]);
  47.                     p[j]=c[j];
  48.                 }
  49.             }
  50.             sb.append(ans+"\n");
  51.         }
  52.         System.out.print(sb);
  53.     }
  54.  
  55.     static class FastReader
  56.     {
  57.         final private int BUFFER_SIZE=1<<16;
  58.         private DataInputStream dis;
  59.         private byte[] buffer;
  60.         private int bufferPointer,bytesRead;
  61.  
  62.         public FastReader()
  63.         {
  64.             dis=new DataInputStream(System.in);
  65.             buffer=new byte[BUFFER_SIZE];
  66.             bufferPointer=bytesRead=0;
  67.         }
  68.  
  69.         public int nextInt() throws IOException
  70.         {
  71.             int ret=0;
  72.             byte c=read();
  73.             while(c<=' ') c=read();
  74.             boolean neg=(c=='-');
  75.             if(neg) c=read();
  76.             do
  77.             {
  78.                 ret=ret*10+c-'0';
  79.             }while((c=read())>='0' && c<='9');
  80.             if(neg) return -ret;
  81.             return ret;
  82.         }
  83.  
  84.         private void fillBuffer()throws IOException
  85.         {
  86.             bytesRead=dis.read(buffer,bufferPointer=0,BUFFER_SIZE);
  87.             if(bytesRead==-1) buffer[0]=-1;
  88.         }
  89.         private byte read() throws IOException
  90.         {
  91.             if(bufferPointer==bytesRead) fillBuffer();
  92.             return buffer[bufferPointer++];
  93.         }
  94.     }
  95. }
Add Comment
Please, Sign In to add comment