http://www.lydsy.com/JudgeOnline/problem.php?id=1055
题意:
思路:
记忆化搜索。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<vector> #include<stack> #include<queue> #include<cmath> #include<map> #include<set> using namespace std; typedef long long ll; typedef pair<int,int> pll; const int INF = 0x3f3f3f3f; const int maxn = 200+5; int n; int num[5]; char s[5]={'W','I','N','G'}; char a[5][20][2]; char str[maxn]; int dp[maxn][maxn][30]; int mp[300]; int solve(int l, int r, int k) { int& ans = dp[l][r][k]; if(ans!=-1) return ans; if(l==r) return dp[l][r][k]=(s[k]==str[l]); for(int i=l;i<r;i++) { for(int j=0;j<num[k];j++) { ans = solve(l,i,mp[a[k][j][0]]) && solve(i+1,r,mp[a[k][j][1]]); if(ans==1) break; } if(ans==1) break; } if(ans==1) return 1; else return 0; } int main() { //freopen("in.txt","r",stdin); memset(dp,-1,sizeof(dp)); mp['W']=0; mp['I']=1; mp['N']=2; mp['G']=3; for(int i=0;i<4;i++) scanf("%d",&num[i]); for(int i=0;i<4;i++) for(int j=0;j<num[i];j++) scanf("%s",a[i][j]); scanf("%s",str+1); n = strlen(str+1); bool flag = false; if(solve(1,n,0)) printf("W"),flag=true; if(solve(1,n,1)) printf("I"),flag=true; if(solve(1,n,2)) printf("N"),flag=true; if(solve(1,n,3)) printf("G"),flag=true; if(flag) puts(""); if(!flag) puts("The name is wrong!"); return 0; }