很显然的区间DP,定义dp[i][j][k], 如果dp[i][j][k]=1表示字符串[i,j]可以组成k字符。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-9 # define MOD 12345678 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())==‘-‘) flag=1; else if(ch>=‘0‘&&ch<=‘9‘) res=ch-‘0‘; while((ch=getchar())>=‘0‘&&ch<=‘9‘) res=res*10+(ch-‘0‘); return flag?-res:res; } void Out(int a) { if(a<0) {putchar(‘-‘); a=-a;} if(a>=10) Out(a/10); putchar(a%10+‘0‘); } const int N=1005; //Code begin... struct To{int a[18][2];}to[5]; int dp[205][205][5], p[30], len, a[5]; char wing[205]; void init(){p[‘W‘-‘A‘]=1; p[‘I‘-‘A‘]=2; p[‘N‘-‘A‘]=3; p[‘G‘-‘A‘]=4; mem(dp,-1);} void dfs(int l, int r) { if (~dp[l][r][0]) return ; if (l==r) {dp[l][r][0]=1; dp[l][r][p[wing[l]-‘A‘]]=1; return ;} FO(i,l,r) { dfs(l,i); dfs(i+1,r); FOR(j,1,4) FOR(k,1,a[j]) { int tmp1=to[j].a[k][0], tmp2=to[j].a[k][1]; if (dp[l][i][tmp1]==1&&dp[i+1][r][tmp2]==1) {dp[l][r][j]=1; break;} } } dp[l][r][0]=1; return ; } int main () { char s[5]; init(); scanf("%d%d%d%d",a+1,a+2,a+3,a+4); FOR(i,1,a[1]) scanf("%s",s), to[1].a[i][0]=p[s[0]-‘A‘], to[1].a[i][1]=p[s[1]-‘A‘]; FOR(i,1,a[2]) scanf("%s",s), to[2].a[i][0]=p[s[0]-‘A‘], to[2].a[i][1]=p[s[1]-‘A‘]; FOR(i,1,a[3]) scanf("%s",s), to[3].a[i][0]=p[s[0]-‘A‘], to[3].a[i][1]=p[s[1]-‘A‘]; FOR(i,1,a[4]) scanf("%s",s), to[4].a[i][0]=p[s[0]-‘A‘], to[4].a[i][1]=p[s[1]-‘A‘]; scanf("%s",wing+1); len=strlen(wing+1); dfs(1,len); int flag=0; if (dp[1][len][1]==1) flag=1, putchar(‘W‘); if (dp[1][len][2]==1) flag=1, putchar(‘I‘); if (dp[1][len][3]==1) flag=1, putchar(‘N‘); if (dp[1][len][4]==1) flag=1, putchar(‘G‘); if (!flag) {puts("The name is wrong!"); return 0;} putchar(‘\n‘); return 0; }
时间: 2024-10-15 14:53:47