标签:
1、就是一个字母可以分裂成另外两个字母,问一个字符串可以由哪个字母搞出来(一共只有四个字母2333)。
2、分析:动态规划,
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 210
inline int read(){
char ch = getchar(); int x = 0, f = 1;
while(ch < ‘0‘ || ch > ‘9‘){
if(ch == ‘-‘) f = -1;
ch = getchar();
}
while(‘0‘ <= ch && ch <= ‘9‘){
x = x * 10 + ch - ‘0‘;
ch = getchar();
}
return x * f;
}
char p[4];
int q[255], t[4];
char ch[M], a[4][20][2];
int f[M][M][4];
inline bool dp(int l, int r, int k){
if(l == r) return (ch[l] == p[k]);
int &ret = f[l][r][k];
if(ret != -1) return ret;
for(int i = 1; i <= t[k]; i ++){
for(int j = l; j <= r - 1; j ++){
if(dp(l, j, q[a[k][i][0]]) && dp(j + 1, r, q[a[k][i][1]])){
return ret = 1;
}
}
}
return ret = 0;
}
int main(){
//freopen("0input.in", "r", stdin);
memset(f, -1, sizeof(f));
p[0] = ‘W‘; p[1] = ‘I‘; p[2] = ‘N‘; p[3] = ‘G‘;
q[‘W‘] = 0; q[‘I‘] = 1; q[‘N‘] = 2; q[‘G‘] = 3;
for(int i = 0; i < 4; i ++) t[i] = read();
for(int i = 0; i < 4; i ++){
for(int j = 1; j <= t[i]; j ++){
scanf("%s", a[i][j]);
}
}
scanf("%s", ch + 1);
int n = strlen(ch + 1);
bool flag = 0;
for(int i = 0; i < 4; i ++){
if(dp(1, n, i)) flag = 1, printf("%c", p[i]);
}
if(!flag) puts("The name is wrong!");
return 0;
}
标签:
原文地址:http://blog.csdn.net/qzh_1430586275/article/details/52276629