亚马逊在线笔试题第二题


package com.zju.edu.cn.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Test {
	
	static int getMatchCount(String[] patterns, String input){
		int len = patterns.length;
		if(len == 0)
			return 0;
		ArrayList<String> array = new ArrayList<String>();
	    for(int i  = 0 ; i < len ; i ++){
	    	if(patterns[i].contains("*")){
	    		for(int j = 0 ; j < 26 ; j ++){
	    			String temp = patterns[i].replace('*',(char)('a' +j)).toLowerCase();
	    			array.add(temp);
	    		}
	    		String temp = patterns[i].replace('*',' ').toLowerCase().trim();
    			array.add(temp);
	    	}
	    	else
	    	    array.add(patterns[i]);
	    }
	    while(input.contains(".") || input.contains(",") || input.contains("!")){
	    	 StringBuilder string = new StringBuilder(input);
	    	int index = string.indexOf(".");
	    	if(index != -1){
	    	    string.insert(string.indexOf("."), " ");
	    		string.deleteCharAt(string.indexOf("."));
	    	}
	    	index = string.indexOf(",");
	    	if(index != -1){
	    	    string.insert(string.indexOf(","), " ");
	    		string.deleteCharAt(string.indexOf(","));
	    	}
	    	index = string.indexOf("!");
	    	if(index != -1){
	    	    string.insert(string.indexOf("!"), " ");
	    		string.deleteCharAt(string.indexOf("!"));
	    	}
	    	input = string.toString();
	    }
	    String list[] =input.split(" ");
	    len = list.length;
	    int count = 0;
	    for(int i = 0 ; i < len ; i ++){
	    	String str = list[i];
	    	if(array.contains(str.toLowerCase())){
	    		count ++;
	    	}
	    	else{
	    		for(int j = 0 ; j < array.size(); j ++){
	    			if(str.toLowerCase().contains(array.get(j))){
	    				count ++;
	    				break;
	    			}
	    		}
	    	}
	    }
		return count;
	}

   
	   
	
	
	    @SuppressWarnings("resource")
		public static void main(String[] args) throws Exception { 
	    	File file = new File("D:\\in.txt");
			InputStream is = new FileInputStream(file);
			
		
//	    	BufferedReader reader = new BufferedReader(new InputStreamReader(is));
//			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//			Scanner in = new Scanner(System.in);
			Scanner in = new Scanner(new InputStreamReader(is));
	    	int num;
	    	num = in.nextInt();
            String patterns[] = new String[num];
            in.nextLine();
            for(int i = 0 ; i < num ; i ++){
            	patterns[i] = in.nextLine(); 
            }
            String input = in.nextLine();
            int count = getMatchCount(patterns,input);
            System.out.println(count);
			
//			System.out.println("matching".contains("match"));
            
            



	    }
}

你可能感兴趣的:(亚马逊在线笔试题第二题)