osipyonok

НКА -> ДКА

Jan 4th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.     static int A;
  11.     static Set<String> S;
  12.     static Set<String> F;
  13.     static Map<Pair<String , String> , Set<String>> f;
  14.    
  15.     public static void main (String[] args) throws java.lang.Exception
  16.     {
  17.         Scanner sc = new Scanner(System.in);
  18.         S = new HashSet();
  19.         F = new HashSet();
  20.         f = new HashMap();
  21.         A = sc.nextInt();
  22.         int s = sc.nextInt();
  23.         ArrayList<String> tmp = new ArrayList();
  24.         for(Integer i = 0 ; i < s ; ++i){
  25.             S.add(i.toString());
  26.             tmp.add(i.toString());
  27.         }
  28.         Integer t_ = sc.nextInt();
  29.         --t_;
  30.         String s0 = ""; s0 += t_;
  31.         int sz_f = sc.nextInt();
  32.         for(int i = 0 ; i < sz_f ; ++i){
  33.             Integer t = sc.nextInt();
  34.             --t;
  35.             F.add(t.toString());
  36.         }
  37.         while(sc.hasNext()){
  38.             Integer u = sc.nextInt();
  39.             String ch = sc.next();
  40.             Integer v = sc.nextInt();
  41.             --u;
  42.             --v;
  43.             if(!f.containsKey(new Pair(u.toString() , ch))){
  44.                 f.put(new Pair(u.toString() , ch) , new HashSet<String>());
  45.             }
  46.             f.get(new Pair(u.toString() , ch)).add(v.toString());
  47.         }
  48.        
  49.         Set<String> new_S = new HashSet();
  50.         Set<String> new_F = new HashSet();
  51.        
  52.         for(int mask = 1 ; mask < Math.pow(2 , s) ; ++mask){
  53.             String cur = "";
  54.             boolean ck = false;
  55.             for(int j = 0 ; j < s ; ++j){
  56.                 if(((mask >> j) & 1) != 0){
  57.                     if(cur.length() > 0){
  58.                         cur += ',';
  59.                     }
  60.                     cur += tmp.get(j);
  61.                     if(F.contains(tmp.get(j))){
  62.                         ck = true;
  63.                     }
  64.                 }
  65.             }
  66.             new_S.add(cur);
  67.             if(ck){
  68.                 new_F.add(cur);
  69.             }
  70.         }
  71.         String new_s0 = s0;
  72.         Map<Pair<String , String> , String> new_f = new HashMap();
  73.         for(String st : new_S){
  74.             String[] states = st.split(",");
  75.             for(char c = 'a' ; c < 'a' + A ; ++c){
  76.                 String temp = ""; temp += c;
  77.                 String to = "";
  78.                 Map<String , Boolean> used = new HashMap();
  79.                 for(int i = 0 ; i < states.length ; ++i){
  80.                     if(!f.containsKey(new Pair<String , String>(states[i] , temp))){
  81.                         continue;
  82.                     }
  83.                     Set<String> vs = f.get(new Pair<String , String>(states[i] , temp));
  84.                     for(String v : vs){
  85.                         if(used.containsKey(v)){
  86.                             continue;
  87.                         }
  88.                         if(to.length() > 0)to += ',';
  89.                         to += v;
  90.                         used.put(v , true);
  91.                     }
  92.                 }
  93.                 if(to.length() > 0)new_f.put(new Pair<String , String>(st , temp) , to);
  94.             }
  95.         }
  96.         System.out.println(A);
  97.         System.out.println(new_S.size());
  98.         System.out.println(new_S);
  99.         System.out.println(new_s0 + 1);
  100.         System.out.println(new_F.size());
  101.         System.out.println(new_F);
  102.         System.out.println(new_f);
  103.     }
  104. }
  105.  
  106. class Pair<T1 extends Comparable<T1> , T2  extends Comparable<T2> > implements Comparable<Pair<T1,T2>>{
  107.     T1 x;
  108.     T2 y;
  109.     Pair(T1 _x , T2 _y){
  110.         x = _x;
  111.         y = _y;
  112.     }
  113.     T1 first(){
  114.         return x;
  115.     }
  116.     T2 second(){
  117.         return y;
  118.     }
  119.     public final Comparator<Pair<T1, T2>> KEY_COMPARATOR = new Comparator<Pair<T1, T2>>() {
  120.         public int compare(Pair<T1, T2> first, Pair<T1, T2> second) {
  121.             if(first.first().equals(second.first()))
  122.                 return first.second().compareTo(second.second());
  123.             return  first.first().compareTo(second.first());
  124.         }
  125.     };
  126.  
  127.     @Override
  128.     public int compareTo(Pair<T1,T2> other){
  129.         if(first().equals(other.first()))
  130.             return second().compareTo(other.second());
  131.         return  first().compareTo(other.first());
  132.     }
  133.     public boolean equals(Object other){
  134.         if (other instanceof Pair) {
  135.             Pair otherPair = (Pair) other;
  136.             return
  137.                     ((  this.x == otherPair.x ||
  138.                             ( this.x != null && otherPair.x != null &&
  139.                                     this.x.equals(otherPair.x))) &&
  140.                             (   this.y == otherPair.y ||
  141.                                     ( this.y != null && otherPair.y != null &&
  142.                                             this.y.equals(otherPair.y))) );
  143.         }
  144.         return false;
  145.     }
  146.  
  147.     public String toString(){
  148.         return "{" + x.toString() + " ; " + y.toString() + "}";
  149.     }
  150.  
  151.     public int hashCode(){
  152.         int hash = 31;
  153.         int res = 1;
  154.         res = hash * res + x.hashCode();
  155.         res = hash * res + y.hashCode();
  156.         return res;
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment