Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Map;
- import java.util.Queue;
- import java.util.StringTokenizer;
- import java.util.ArrayList;
- import java.util.TreeSet;
- public class Main implements Runnable {
- StringTokenizer st;
- BufferedReader in;
- PrintWriter out;
- String s;
- int color[];
- Queue<Integer> Q;
- ArrayList<TreeSet<Character>>[] pr;
- static int POW = 'z' - 'a' + 2;
- int n, m, k;
- int cur = 0;
- TreeSet<Character> states;
- int ok[];
- int color2[];
- int leftsInUse[];
- int color3[];
- char lefts[];
- // //////////////////////////////////////////////////////
- public static void main(String[] args) {
- new Thread(new Main()).start();
- }
- @SuppressWarnings("unchecked")
- void supMov(int a) {
- for (int k=0; k<pr[a].size(); k++) {
- Q.addAll((TreeSet)pr[a].get(k));
- }
- }
- public void run() {
- try {
- in = new BufferedReader(new FileReader("useless.in"));
- out = new PrintWriter(new FileWriter("useless.out"));
- solve();
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(9000);
- } finally {
- out.flush();
- out.close();
- }
- }
- String nextToken() throws IOException {
- while (st == null || !st.hasMoreTokens()) {
- st = new StringTokenizer(in.readLine());
- }
- return st.nextToken();
- }
- int nextInt() throws NumberFormatException, IOException {
- return Integer.parseInt(nextToken());
- }
- long nextLong() throws NumberFormatException, IOException {
- return Long.parseLong(nextToken());
- }
- double nextDouble() throws NumberFormatException, IOException {
- return Double.parseDouble(nextToken());
- }
- @SuppressWarnings("unchecked")
- void solve() throws NumberFormatException, IOException {
- n = nextInt();
- pr=new ArrayList[POW];
- ok=new int[POW];
- color3=new int[POW];
- for (int i=0; i<POW; i++) pr[i]=new ArrayList<TreeSet<Character>>();
- color=new int[POW];
- color2=new int[POW];
- char start = (char) (nextToken().charAt(0)-'@');
- for (int i = 0; i < n; i++) {
- char t = (char) (nextToken().charAt(0)-'@');
- String s = nextToken();
- color2[t]=1;
- if (st.hasMoreTokens()) {
- s = nextToken();
- TreeSet<Character> T= new TreeSet();
- char let[] = s.toCharArray();
- for (char c : let) {
- if ((c>='A')&&(c<='Z')){
- T.add(c);
- color3[c-'@']=1;
- }
- }
- pr[t].add(T);
- } else {
- TreeSet<Character> T = new TreeSet();
- T.add('@');
- pr[t].add(T);
- }
- }
- Q=new LinkedList();
- Q.add((int)start);
- color[start]=1;
- while(!Q.isEmpty()) {
- int a= Q.remove();
- if (color[a]!=1){
- supMov(a);
- color[a]=1;
- }
- }
- for (int i=1; i<POW; i++) {
- if ((color[i]==0)&&(color2[i]==0)&&(color3[i]!=0)) out.print((char)(i+'@')+" ");
- }
- }
- }
Add Comment
Please, Sign In to add comment