Advertisement
osipyonok

Допускает ли НКА слово

Jan 4th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 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 , s , s0 , sz_f;
  11.     static Set<Integer> F;
  12.     static Map<Pair<Integer , Character> , ArrayList<Integer>> f;
  13.    
  14.     public static void main (String[] args) throws java.lang.Exception
  15.     {
  16.         Scanner sc = new Scanner(System.in);
  17.         String ck = sc.next();
  18.         a = sc.nextInt();
  19.         s = sc.nextInt();
  20.         s0 = sc.nextInt();
  21.         sz_f = sc.nextInt();
  22.         F = new HashSet<Integer>();
  23.         for(int i = 0 ; i < sz_f ; ++i){
  24.             F.add(sc.nextInt());
  25.         }
  26.         f = new HashMap();
  27.         while(sc.hasNext()){
  28.             int u = sc.nextInt();
  29.             char ch = sc.next().charAt(0);
  30.             int v = sc.nextInt();
  31.             if(!f.containsKey(new Pair<Integer , Character>(u , ch))){
  32.                 f.put((new Pair<Integer , Character>(u , ch)), new ArrayList<Integer>());
  33.             }
  34.             ArrayList<Integer> tmp = f.get(new Pair<Integer , Character>(u , ch));
  35.             tmp.add(v);
  36.             f.put(new Pair<Integer , Character>(u , ch), tmp);
  37.         }
  38.         boolean ans = false;
  39.         Queue<Pair<Integer, Integer>> q = new PriorityQueue();
  40.         q.add(new Pair<Integer,Integer>(s0 , -1));
  41.         while(q.size() > 0){
  42.             Pair<Integer,Integer> cur = q.element();
  43.             q.remove();
  44.             int cur_ch = cur.second() + 1;
  45.             int cur_st = cur.first();
  46.             if(cur_ch == ck.length()){
  47.                 ans = true;
  48.                 break;
  49.             }
  50.             if(f.containsKey(new Pair<Integer , Character>(cur_st,ck.charAt(cur_ch)))){
  51.                 ArrayList<Integer> tmp = f.get(new Pair<Integer , Character>(cur_st,ck.charAt(cur_ch)));
  52.                 for(Integer v : tmp){
  53.                     q.add(new Pair<Integer,Integer>(v , cur_ch));
  54.                 }
  55.             }
  56.         }
  57.         System.out.println(ans);
  58.     }
  59.    
  60.    
  61. }
  62.  
  63. class Pair<T1 , T2> {
  64.     T1 x;
  65.     T2 y;
  66.     Pair(T1 _x , T2 _y){
  67.         x = _x;
  68.         y = _y;
  69.     }
  70.     T1 first(){
  71.         return x;
  72.     }
  73.     T2 second(){
  74.         return y;
  75.     }
  76.    
  77.     public boolean equals(Object other){
  78.         if (other instanceof Pair) {
  79.             Pair otherPair = (Pair) other;
  80.             return
  81.             ((  this.x == otherPair.x ||
  82.                 ( this.x != null && otherPair.x != null &&
  83.                   this.x.equals(otherPair.x))) &&
  84.              (  this.y == otherPair.y ||
  85.                 ( this.y != null && otherPair.y != null &&
  86.                   this.y.equals(otherPair.y))) );
  87.         }
  88.         return false;
  89.     }
  90.    
  91.     public int hashCode(){ 
  92.         int hash = 31;
  93.         int res = 1;
  94.         res = hash * res + x.hashCode();
  95.         res = hash * res + y.hashCode();
  96.         return res;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement