Description
某人有一套玩具,并想法给玩具命名。首先他选择WING四个字母中的任意一个字母作为玩具的基本名字。然后
他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够扩充得很长。
现在,他想请你猜猜某一个很长的名字,最初可能是由哪几个字母变形过来的。
Input
第一行四个整数W、I、N、G。表示每一个字母能由几种两个字母所替代。接下来W行,每行两个字母,表示W可
以用这两个字母替代。接下来I行,每行两个字母,表示I可以用这两个字母替代。接下来N行,每行两个字母,表示N
可以用这两个字母替代。接下来G行,每行两个字母,表示G可以用这两个字母替代。最后一行一个长度不超过Len的
字符串。表示这个玩具的名字。
Output
一行字符串,该名字可能由哪些字母变形而得到。(按照WING的顺序输出)如果给的名字不能由任何一个字母
变形而得到则输出“The name is wrong!”
Sample Input
1 1 1 1
II
WW
WW
IG
IIII
II
WW
WW
IG
IIII
Sample Output
IN
HINT
W可以变成II所以IIII可以缩成WW IN均能变成WW所以WW又可以缩成I或者N 所以最终答案应该按照“WING”的顺序
输出IN
[数据范围]
100%数据满足Len<=200,W、I、N、G<=16
感觉像是一个类似于背包DP表示该方案是否可行。。
如果i~j可以用p[n]到达,j+1~k可以用p[m]到达,那么i~k可以用p[n]p[m]到达,然后找p[n]p[m]由什么构成即可。。。
注意,同一个p[n]p[m]可能由多种字符得到。。。
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 10000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int n[5],len,ans; 24 vector<int> a[5][5]; 25 bool f[300][300][5]; 26 char s1[4]={'W','I','N','G'}; 27 string s; 28 int read(){ 29 int x=0,f=1;char ch=getchar(); 30 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 31 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 32 return x*f; 33 } 34 int hash(char ch){ 35 if(ch=='W')return 1; 36 if(ch=='I')return 2; 37 if(ch=='N')return 3; 38 return 4; 39 } 40 int main(){ 41 //freopen("input.txt","r",stdin); 42 //freopen("output.txt","w",stdout); 43 for1(i,4)n[i]=read(); 44 for1(i,4) 45 for1(j,n[i]){ 46 char ch=getchar(); 47 while(ch!='W'&&ch!='I'&&ch!='N'&&ch!='G')ch=getchar(); 48 char ch2=getchar(); 49 a[hash(ch)][hash(ch2)].push_back(i); 50 } 51 getline(cin,s);getline(cin,s); 52 len=s.length(); 53 for1(i,len)f[i][i][hash(s[i-1])]=1; 54 for(int l=2;l<=len;l++) 55 for1(i,len-l+1){ 56 int j=i+l-1; 57 for(int k=i;k<=j-1;k++) 58 for1(p,4) 59 if(f[i][k][p]) 60 for1(q,4) 61 if(f[k+1][j][q]) 62 for(int t=0;t<a[p][q].size();t++) 63 f[i][j][a[p][q][t]]=1; 64 } 65 for1(i,4)if(f[1][len][i])ans++,cout<<s1[i-1]; 66 if(ans==0)cout<<"The name is wrong!"; 67 return 0; 68 }