IOI的。。。暴力的动规。。。以后记得不要把DP想得太复杂。。。
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(i=l;i<=r;i++) 3 #define dec(i,l,r) for(i=l;i>=r;i--) 4 #define inf 1e9 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define ll long long 7 #define succ(x) (1<<x) 8 #define NM 100000+5 9 using namespace std; 10 int read(){ 11 int x=0,f=1;char ch=getchar(); 12 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 13 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 14 return x*f; 15 } 16 char s[NM]; 17 int f[4][4][4][4],g[4][4][4][4],i,x1,x2,v,n,y2,t,ans; 18 int add(int v,int x,int y){ 19 if(x==0)x=v; 20 if(y==0)y=v; 21 if(x==y&&x==v)return 1; 22 if(x==y||x==v||y==v)return 2; 23 return 3; 24 } 25 int r(char x){ 26 if(x=='M')return 1; 27 if(x=='B')return 2; 28 return 3; 29 } 30 int main(){ 31 int y1; 32 n=read(); 33 scanf("%s",s); 34 g[r(s[0])][0][0][0]=g[0][0][r(s[0])][0]=1; 35 inc(i,1,n-1){ 36 v=r(s[i]);mem(f); 37 inc(x1,0,3) 38 inc(x2,0,3) 39 inc(y1,0,3) 40 inc(y2,0,3) 41 if(t=g[x1][x2][y1][y2]){ 42 // printf("%d:%d %d %d %d %d\n",i,x1,x2,y1,y2,t); 43 f[v][x1][y1][y2]=max(f[v][x1][y1][y2],t+add(v,x1,x2)); 44 f[x1][x2][v][y1]=max(f[x1][x2][v][y1],t+add(v,y1,y2)); 45 } 46 inc(x1,0,3) 47 inc(x2,0,3) 48 inc(y1,0,3) 49 inc(y2,0,3) 50 g[x1][x2][y1][y2]=f[x1][x2][y1][y2]; 51 } 52 inc(x1,0,3) 53 inc(x2,0,3) 54 inc(y1,0,3) 55 inc(y2,0,3) 56 ans=max(ans,f[x1][x2][y1][y2]); 57 printf("%d\n",ans); 58 return 0; 59 }