双生词-字节跳动的笔试

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        String []mystr=new String[n];
        for(int i=0;i<n;i++){
            int n1=sc.nextInt();
            String []str=new String[n1];
            for(int j=0;j<n1;j++){
                str[j]=sc.next();
            }
            
            if(solve(str)) mystr[i]="Yeah";
            else mystr[i]="Sad";
        }
        for(int i=0;i<n;i++){
            System.out.println(mystr[i]);
        }
    }
    
    public static boolean solve(String []str){
        for(int i=0;i<str.length-1;i++)
            for(int j=i+1;j<str.length;j++){
                if(str[i].length()==str[j].length())
                if(find(str[i],str[j])) return true;
            }
        return false;
    }
    public static boolean find(String parent,String child){
        String parent1=parent+parent;
        Pattern p = Pattern.compile( child );
        Matcher m = p.matcher(parent1);
        
        if(m.find()) return true;
        String child1=new StringBuilder(child).reverse().toString();
        //System.out.println(child1);
        p = Pattern.compile(child1);
        m = p.matcher(parent1);
        if(m.find()) return true;
        return false;
    }
    
}
 

猜你喜欢

转载自blog.csdn.net/boguesfei/article/details/82052728