Guest User

Untitled

a guest
Aug 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6.  
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.Map;
  11. import java.util.Queue;
  12. import java.util.StringTokenizer;
  13. import java.util.ArrayList;
  14. import java.util.TreeSet;
  15.  
  16. public class Main implements Runnable {
  17. StringTokenizer st;
  18. BufferedReader in;
  19. PrintWriter out;
  20. String s;
  21. int color[];
  22. Queue<Integer> Q;
  23. ArrayList<TreeSet<Character>>[] pr;
  24. static int POW = 'z' - 'a' + 2;
  25. int n, m, k;
  26. int cur = 0;
  27. TreeSet<Character> states;
  28. int ok[];
  29. int color2[];
  30. int leftsInUse[];
  31. int color3[];
  32. char lefts[];
  33.  
  34.  
  35. // //////////////////////////////////////////////////////
  36. public static void main(String[] args) {
  37. new Thread(new Main()).start();
  38. }
  39. @SuppressWarnings("unchecked")
  40. void supMov(int a) {
  41. for (int k=0; k<pr[a].size(); k++) {
  42. Q.addAll((TreeSet)pr[a].get(k));
  43. }
  44. }
  45. public void run() {
  46. try {
  47. in = new BufferedReader(new FileReader("useless.in"));
  48. out = new PrintWriter(new FileWriter("useless.out"));
  49. solve();
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. System.exit(9000);
  53. } finally {
  54. out.flush();
  55. out.close();
  56. }
  57.  
  58. }
  59.  
  60. String nextToken() throws IOException {
  61. while (st == null || !st.hasMoreTokens()) {
  62. st = new StringTokenizer(in.readLine());
  63. }
  64. return st.nextToken();
  65. }
  66.  
  67. int nextInt() throws NumberFormatException, IOException {
  68. return Integer.parseInt(nextToken());
  69. }
  70.  
  71. long nextLong() throws NumberFormatException, IOException {
  72. return Long.parseLong(nextToken());
  73. }
  74.  
  75. double nextDouble() throws NumberFormatException, IOException {
  76. return Double.parseDouble(nextToken());
  77. }
  78.  
  79. @SuppressWarnings("unchecked")
  80. void solve() throws NumberFormatException, IOException {
  81. n = nextInt();
  82. pr=new ArrayList[POW];
  83. ok=new int[POW];
  84. color3=new int[POW];
  85. for (int i=0; i<POW; i++) pr[i]=new ArrayList<TreeSet<Character>>();
  86.  
  87.  
  88. color=new int[POW];
  89. color2=new int[POW];
  90.  
  91. char start = (char) (nextToken().charAt(0)-'@');
  92. for (int i = 0; i < n; i++) {
  93. char t = (char) (nextToken().charAt(0)-'@');
  94. String s = nextToken();
  95. color2[t]=1;
  96. if (st.hasMoreTokens()) {
  97. s = nextToken();
  98. TreeSet<Character> T= new TreeSet();
  99. char let[] = s.toCharArray();
  100. for (char c : let) {
  101. if ((c>='A')&&(c<='Z')){
  102. T.add(c);
  103. color3[c-'@']=1;
  104. }
  105. }
  106. pr[t].add(T);
  107. } else {
  108. TreeSet<Character> T = new TreeSet();
  109.  
  110. T.add('@');
  111. pr[t].add(T);
  112.  
  113.  
  114. }
  115.  
  116. }
  117. Q=new LinkedList();
  118. Q.add((int)start);
  119. color[start]=1;
  120. while(!Q.isEmpty()) {
  121. int a= Q.remove();
  122. if (color[a]!=1){
  123. supMov(a);
  124. color[a]=1;
  125. }
  126. }
  127. for (int i=1; i<POW; i++) {
  128. if ((color[i]==0)&&(color2[i]==0)&&(color3[i]!=0)) out.print((char)(i+'@')+" ");
  129. }
  130.  
  131.  
  132. }
  133. }
Add Comment
Please, Sign In to add comment