题解中都是以一个三维数组g[z][x][y]代表xy能拼出z, 我用g[x][y]=z的形式做,若拼不出值显然为0,不影响答案,但代码是60分,请问是思路有问题吗?
#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,d,f[5][205][205],g[5][5];
char ch[205],s[3];
int zh(char ch)
{
if(ch=='W')
return 1;
if(ch=='I')
return 2;
if(ch=='N')
return 3;
if(ch=='G')
return 4;
return 0;
}
int main()
{
scanf("%d%d%d%d",&a,&b,&c,&d);
for(int i=1;i<=a;i++)
scanf("%s",s),g[zh(s[0])][zh(s[1])]=1;
for(int i=1;i<=b;i++)
scanf("%s",s),g[zh(s[0])][zh(s[1])]=2;
for(int i=1;i<=c;i++)
scanf("%s",s),g[zh(s[0])][zh(s[1])]=3;
for(int i=1;i<=d;i++)
scanf("%s",s),g[zh(s[0])][zh(s[1])]=4;
scanf("%s",ch+1);
n=strlen(ch+1);
for(int i=1;i<=n;i++)
f[zh(ch[i])][i][i]=1;
for(int len=2;len<=n;len++)
for(int i=1;i<=n-len+1;i++)
{
int j=i+len-1;
for(int k=i;k<j;k++)
for(int x=1;x<=4;x++)
for(int y=1;y<=4;y++)
if(f[x][i][k]&&f[y][k+1][j])
f[g[x][y]][i][j]=1;
}
bool pd=false;
if(f[1][1][n])
printf("W"),pd=true;
if(f[2][1][n])
printf("I"),pd=true;
if(f[3][1][n])
printf("N"),pd=true;
if(f[4][1][n])
printf("G"),pd=true;
if(!pd)
printf("The name is wrong!");
return 0;
}