Pages

Wednesday 31 October 2012

UVA - 10679 - I Love Strings!!

/*Well one of the best solution to this problem is  Aho Corasick algorithm.
It will get better run-time  than my implemented algorithm*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int len=Integer.parseInt(br.readLine());
        for(int i=0;i<len;i++) {
            String test=br.readLine();
            int q=Integer.parseInt(br.readLine());
            for(int j=0;j<q;j++) {
                String get=br.readLine();
                if(!findPattern(get, test)){
                    sb.append("n\n");
                }else{
                    sb.append("y\n");
                }
            }
        }
        System.out.print(sb);
    }
   
    static boolean findPattern(String Pattern,String str){
        int count=0;
        for(int i=0,j=0;i<str.length();i++){
            if(Pattern.charAt(j)==str.charAt(i)){
                count++;
                j++;
            }else{
                j=0;
                count=0;
            }
            if(count==Pattern.length()){
                return true;
            }
        }
        return false;
    }
}

No comments:

Post a Comment