题目描述:
中文题不解释题意,这题DP可以说是十分暴力的,先把所有可以压缩的情况预处理好,然后对于每个区间直接暴力枚举每种情况,注意用|运算,这样只要有一种情况符合要求就会记为1,最后检验一下四种情况是否可以即可。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<string>
#include<cmath>
#include<stack>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int MAXM=200;
const long long MOD=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1);
struct pt
{
char ch1,ch2;
char ch;
};
int W,I,N,G;
string s;
struct pt p[210];
bool dp[210][210][210];
int main()
{
scanf("%d%d%d%d",&W,&I,&N,&G);
int cnt=1;
for (int i=1;i<=W;i++)
{
cin>>s;
p[cnt].ch1=s[0];p[cnt].ch2=s[1];p[cnt].ch='W';
cnt++;
}
for (int i=1;i<=I;i++)
{
cin>>s;
p[cnt].ch1=s[0];p[cnt].ch2=s[1];p[cnt].ch='I';
cnt++;
}
for (int i=1;i<=N;i++)
{
cin>>s;
p[cnt].ch1=s[0];p[cnt].ch2=s[1];p[cnt].ch='N';
cnt++;
}
for (int i=1;i<=G;i++)
{
cin>>s;
p[cnt].ch1=s[0];p[cnt].ch2=s[1];p[cnt].ch='G';
cnt++;
}
cin>>s;
int len=s.size();
memset(dp,false,sizeof(dp));
for (int i=0;i<len;i++)
dp[i][i][s[i]]=true;
for (int i=1;i<len;i++)
{
for (int j=0;j+i<len;j++)
{
for (int k=j;k<=j+i;k++)
for (int l=1;l<cnt;l++)
dp[j][j+i][p[l].ch]|=(dp[j][k][p[l].ch1]&&dp[k+1][i+j][p[l].ch2]);
}
}
bool flag=false;
if (dp[0][len-1]['W']) cout<<'W',flag=true;
if (dp[0][len-1]['I']) cout<<'I',flag=true;
if (dp[0][len-1]['N']) cout<<'N',flag=true;
if (dp[0][len-1]['G']) cout<<'G',flag=true;
if (!flag) cout<<"The name is wrong!"<<endl;
else cout<<endl;
return 0;
}