fork(1) download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. import static java.lang.Math.*;
  6. import static java.lang.Character.*;
  7. import static java.util.Arrays.*;
  8. import static java.util.Collections.*;
  9. import static java.math.BigInteger.*;
  10.  
  11. class B {
  12. static final char PROB = B.class.getSimpleName().charAt(0);
  13. static final boolean PRAC = false;
  14.  
  15. static final int INF = 1 << 20;
  16. static final int[] di = { -1, 0, 0, 1 };
  17. static final int[] dj = { 0, -1, 1, 0 };
  18. static Scanner sc = new Scanner(System.in);
  19.  
  20. final int C, D, N;
  21. final char[] c1, c2, c3, d1, d2;
  22. final char[] n;
  23.  
  24. public B() {
  25. C = sc.nextInt();
  26. c1 = new char[2 * C];
  27. c2 = new char[2 * C];
  28. c3 = new char[2 * C];
  29. for (int i = 0; i < C; i++) {
  30. char[] cs = sc.next().toCharArray();
  31. c1[2 * i] = c2[2 * i + 1] = cs[0];
  32. c1[2 * i + 1] = c2[2 * i] = cs[1];
  33. c3[2 * i] = c3[2 * i + 1] = cs[2];
  34. }
  35. D = sc.nextInt();
  36. d1 = new char[2 * D];
  37. d2 = new char[2 * D];
  38. for (int i = 0; i < D; i++) {
  39. char[] cs = sc.next().toCharArray();
  40. d1[2 * i] = d2[2 * i + 1] = cs[0];
  41. d1[2 * i + 1] = d2[2 * i] = cs[1];
  42. }
  43. N = sc.nextInt();
  44. n = sc.next().toCharArray();
  45. }
  46.  
  47. public String solve() {
  48. List<Character> list = new ArrayList<Character>();
  49. Set<Character> set = new HashSet<Character>();
  50. Character pre = null;
  51. for (int i = 0; i < N; i++) {
  52. Character c = null;
  53. if (pre != null)
  54. for (int j = 0; j < 2 * C; j++)
  55. if (c1[j] == pre && c2[j] == n[i])
  56. c = c3[j];
  57. if (pre != null && c != null) {
  58. list.set(list.size() - 1, c);
  59. pre = c;
  60. } else {
  61. if (pre != null)
  62. for (int j = 0; j < 2 * D; j++)
  63. if (d1[j] == pre)
  64. set.add(d2[j]);
  65. if (set.contains(n[i])) {
  66. set.clear();
  67. list.clear();
  68. pre = null;
  69. } else {
  70. list.add(n[i]);
  71. pre = n[i];
  72. }
  73. }
  74. }
  75. return list.toString();
  76. }
  77.  
  78. public static void main(String... args) {
  79. // large();
  80. int T = Integer.parseInt(sc.nextLine());
  81. for (int t = 1; t <= T; t++) {
  82. System.err.printf("Case #%s%n", t);
  83. System.out.printf("Case #%s: %s%n", t, new B().solve());
  84. }
  85. System.err.println("done.");
  86. }
  87.  
  88. public static void small() {
  89. String in = PROB + "-small" + (PRAC ? "-practice" : "-attempt" + 0) + ".in";
  90. String out = PROB + "-small.out";
  91. try {
  92. if (!PRAC)
  93. for (int i = 1; new File(PROB + "-small" + "-attempt" + i + ".in").exists(); i++)
  94. in = PROB + "-small" + "-attempt" + i + ".in";
  95. System.setOut(new PrintStream(out));
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. System.exit(0);
  99. }
  100. sc = new Scanner(System.in);
  101. }
  102.  
  103. public static void large() {
  104. String in = PROB + "-large" + (PRAC ? "-practice" : "") + ".in";
  105. String out = PROB + "-large.out";
  106. try {
  107. System.setOut(new PrintStream(out));
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. System.exit(0);
  111. }
  112. sc = new Scanner(System.in);
  113. }
  114.  
  115. private static void debug(Object... os) {
  116. System.err.println(deepToString(os));
  117. }
  118. }
  119.  
Success #stdin #stdout 0.09s 213440KB
stdin
5
0 0 2 EA
1 QRI 0 4 RRQR
1 QFT 1 QF 7 FAQFDFQ
1 EEZ 1 QE 7 QEEEERA
0 1 QW 2 QW
stdout
Case #1: [E, A]
Case #2: [R, I, R]
Case #3: [F, D, T]
Case #4: [Z, E, R, A]
Case #5: []