Given a string consisting of 'w', 'I', 'n', and 'G', these rules can combine two characters into one, for example, "II" can be synthesized into 'w', "ww" can be synthesized into 'I' or 'N'
Which types of characters can this string be finally synthesized?
Seeing this question, I think of broad search... Actually, it is not necessary. It can be completely solved by action.
If f [I] [J] [k] is the J character starting from I, can it be merged into a character [k]
Then J loops from the outer layer, all the remaining preprocessing, how to transfer the violence, I wrote six loops, a little scary
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 210using namespace std;int n,W,I,N,G;int trans[4][4][4];bool f[M][M][4],flag;char s[M],table[5]="WING";inline int GetInt(char c){switch(c){case 'W':return 0;case 'I':return 1;case 'N':return 2;case 'G':return 3;}}inline void Input(int p){int x,y;char c[2];scanf("%s",c);x=GetInt(c[0]);y=GetInt(c[1]);trans[x][y][p]=1;}int main(){int i,j,k,c1,c2,c3;cin>>W>>I>>N>>G;for(i=1;i<=W;i++)Input(0);for(i=1;i<=I;i++)Input(1);for(i=1;i<=N;i++)Input(2);for(i=1;i<=G;i++)Input(3);scanf("%s",s+1);n=strlen(s+1);for(i=1;i<=n;i++)f[i][1][ GetInt(s[i]) ]=1;for(j=2;j<=n;j++)for(i=1;i+j-1<=n;i++)for(k=i;k<i+j-1;k++)for(c1=0;c1<4;c1++)if(f[i][k-i+1][c1])for(c2=0;c2<4;c2++)if(f[k+1][i+j-1-k][c2])for(c3=0;c3<4;c3++)if(trans[c1][c2][c3])f[i][j][c3]=1;for(i=0;i<4;i++)if(f[1][n][i])flag=1,putchar(table[i]);if(!flag)puts("The name is wrong!");}
Bzoj 1055 haoi2008 Dynamic Planning of toys