1055: [HAOI2008]玩具取名
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2047 Solved: 1198
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
II
WW
WW
IG
IIII
Sample Output
HINT
Source
思考
設
最後判定
#include<bits/stdc++.h>
using namespace std;
#define M 205
int W,I,N,G,n,tot;
char s[M];
struct data{
char x,y,z;
}ch[M];
bool f[M][M][5];
inline int idx(char x){
if(x=='W')return 0;
if(x=='I')return 1;
if(x=='N')return 2;
if(x=='G')return 3;
}
char dir[5]={'W','I','N','G'};
inline void read(int &res){
static char ch;int flag=1;
while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48;
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag;
}
int main(){
read(W),read(I),read(N),read(G);
for(register int i=1;i<=W;++i)scanf("%s",s),ch[++tot].x=s[0],ch[tot].y=s[1],ch[tot].z='W';
for(register int i=1;i<=I;++i)scanf("%s",s),ch[++tot].x=s[0],ch[tot].y=s[1],ch[tot].z='I';
for(register int i=1;i<=N;++i)scanf("%s",s),ch[++tot].x=s[0],ch[tot].y=s[1],ch[tot].z='N';
for(register int i=1;i<=G;++i)scanf("%s",s),ch[++tot].x=s[0],ch[tot].y=s[1],ch[tot].z='G';
scanf("%s",s+1),n=strlen(s+1);
for(register int i=1;i<=n;++i)f[i][i][idx(s[i])]=1;
for(register int len=2;len<=n;++len)
for(register int l=1;l<=n-len+1;++l){
int r=l+len-1;
for(register int k=l;k<=r;++k)
for(register int i=1;i<=tot;++i)
f[l][r][idx(ch[i].z)]|=f[l][k][idx(ch[i].x)]&&f[k+1][r][idx(ch[i].y)];
}
bool flag=0;
for(register int i=0;i<4;++i)
if(f[1][n][i])printf("%c",dir[i]),flag=1;
if(!flag)puts("The name is wrong!");
return 0;
}