Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* package whatever; // don't place package name! */
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- /* Name of the class has to be "Main" only if the class is public. */
- class Ideone
- {
- static int a , s , s0 , sz_f;
- static Set<Integer> F;
- static Map<Pair<Integer , Character> , ArrayList<Integer>> f;
- public static void main (String[] args) throws java.lang.Exception
- {
- Scanner sc = new Scanner(System.in);
- String ck = sc.next();
- a = sc.nextInt();
- s = sc.nextInt();
- s0 = sc.nextInt();
- sz_f = sc.nextInt();
- F = new HashSet<Integer>();
- for(int i = 0 ; i < sz_f ; ++i){
- F.add(sc.nextInt());
- }
- f = new HashMap();
- while(sc.hasNext()){
- int u = sc.nextInt();
- char ch = sc.next().charAt(0);
- int v = sc.nextInt();
- if(!f.containsKey(new Pair<Integer , Character>(u , ch))){
- f.put((new Pair<Integer , Character>(u , ch)), new ArrayList<Integer>());
- }
- ArrayList<Integer> tmp = f.get(new Pair<Integer , Character>(u , ch));
- tmp.add(v);
- f.put(new Pair<Integer , Character>(u , ch), tmp);
- }
- boolean ans = false;
- Queue<Pair<Integer, Integer>> q = new PriorityQueue();
- q.add(new Pair<Integer,Integer>(s0 , -1));
- while(q.size() > 0){
- Pair<Integer,Integer> cur = q.element();
- q.remove();
- int cur_ch = cur.second() + 1;
- int cur_st = cur.first();
- if(cur_ch == ck.length()){
- ans = true;
- break;
- }
- if(f.containsKey(new Pair<Integer , Character>(cur_st,ck.charAt(cur_ch)))){
- ArrayList<Integer> tmp = f.get(new Pair<Integer , Character>(cur_st,ck.charAt(cur_ch)));
- for(Integer v : tmp){
- q.add(new Pair<Integer,Integer>(v , cur_ch));
- }
- }
- }
- System.out.println(ans);
- }
- }
- class Pair<T1 , T2> {
- T1 x;
- T2 y;
- Pair(T1 _x , T2 _y){
- x = _x;
- y = _y;
- }
- T1 first(){
- return x;
- }
- T2 second(){
- return y;
- }
- public boolean equals(Object other){
- if (other instanceof Pair) {
- Pair otherPair = (Pair) other;
- return
- (( this.x == otherPair.x ||
- ( this.x != null && otherPair.x != null &&
- this.x.equals(otherPair.x))) &&
- ( this.y == otherPair.y ||
- ( this.y != null && otherPair.y != null &&
- this.y.equals(otherPair.y))) );
- }
- return false;
- }
- public int hashCode(){
- int hash = 31;
- int res = 1;
- res = hash * res + x.hashCode();
- res = hash * res + y.hashCode();
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement