fork download
  1. #include <cstring>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <queue>
  9.  
  10. #define rep(i, l, r) for(int i = l; i <= r; i++)
  11. #define down(i, l, r) for(int i = l; i >= r; i--)
  12. #define MS 234
  13. #define MAX 1037471823
  14.  
  15. using namespace std;
  16.  
  17. int n, v[5];
  18. bool w[5][5][5], dp[MS][MS][5];
  19. char s[MS];
  20.  
  21. int main()
  22. {
  23. scanf("%d%d%d%d", &v[1], &v[2], &v[3], &v[4]);
  24. rep(o, 1, 4) rep(i, 1, v[o])
  25. {
  26. scanf("%s", s);
  27. w[s[0]=='W'?1:s[0]=='I'?2:s[0]=='N'?3:4][s[1]=='W'?1:s[1]=='I'?2:s[1]=='N'?3:4][o] = true;
  28. }
  29. scanf("%s", s); n = strlen(s);
  30. rep(i, 0, n-1) dp[i+1][i+1][s[i]=='W'?1:s[i]=='I'?2:s[i]=='N'?3:4] = 1;
  31. rep(i, 1, n) rep(j, 1, n-i) rep(k, 1, 4)
  32. {
  33. rep(x, 1, 4) rep(y, 1, 4) if (w[x][y][k])
  34. {
  35. rep(o, j, j+i-1) if (dp[j][o][x] && dp[o+1][j+i][y]) { dp[j][j+i][k] = 1; break; }
  36. if (dp[j][j+i][k]) break;
  37. }
  38. }
  39. if (dp[1][n][1] + dp[1][n][2] + dp[1][n][3] + dp[1][n][4] == 0) printf("The name is wrong!");
  40. else
  41. {
  42. if (dp[1][n][1]) printf("W");
  43. if (dp[1][n][2]) printf("I");
  44. if (dp[1][n][3]) printf("N");
  45. if (dp[1][n][4]) printf("G");
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 3612KB
stdin
1 1 1 1
II
WW
WW
IG
IIII
stdout
IN