南大高级算法作业之字符串匹配问题

import java.util.*;


public class Main
{		
	
	public static void main (String[] args){
		
		Scanner scan = new Scanner(System.in);
	
		int e_num = Integer.parseInt(scan.nextLine());
    	
			while(e_num > 0){
				
				String[] str = scan.nextLine().split(","); 
				
				String str1 = str[0];
				
				String target = str[1];
				
				int left = 0;
				
				int index = 0;
				
				int temp = 0;//count of result
				
				int[] result = new int[str1.length()];
				
				while((index+target.length()-1) < str1.length()){
				
					//find the start of the same part
					while(target.charAt(left) != str1.charAt(index)){
						
						index ++;
						
					}
					
					int right = 0;
					
					//find the end of the same part
					while(target.charAt(right) == str1.charAt(index+right) && (index+target.length()-1) < str1.length()){
								
						if(right+1 == target.length()){
									
							result[temp] = index;
							
						//	System.out.print(temp);
							
							temp ++;
							
							break;
								
						}
								
						right ++;
							
					}
					
					index ++;
				
				}
				
				for(int i=0;i < temp;i ++){
					
					if(i == temp){
						
						System.out.print(result[i]);
						
					}else{
						
						System.out.print(result[i]+" ");
						
					}
					
				}
				
				System.out.println();
    				
    			e_num --;
	
			}
	}
       
}

 

你可能感兴趣的:(南大高级算法作业之字符串匹配问题)