(Relax ST1.4)POJ 1056 IMMEDIATE DECODABILITY(判断一个字符串是否是另外一个字符串的前缀)


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class POJ_1056 {

	public static boolean isPrefix(String a,String b){
		int len = a.length() > b.length() ? b.length() : a.length();
		
		if(a.substring(0, len).equals(b.substring(0,len))){
			return true;
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		String temp;
		List<String> list  = new ArrayList<String>();
		int counter = 1;
		while(scanner.hasNext()){
			temp = scanner.next();
			
			if(temp.equals("9")){
				
				int size = list.size();
				
				int i,j;
				boolean flag = true;
				for(i = 0 ; i < size - 1 ; ++i){
					for(j = i+1 ; j < size ; ++j){
						//判断一个字符串是否是另外一个字符串的前缀可以这样做...
						if(list.get(i).startsWith(list.get(j)) || list.get(j).startsWith(list.get(i))){
							flag = false;
							break;
						}
						
//						//也可以写成
//						if(isPrefix(list.get(i), list.get(j))){
//							flag = false;
//							break;
//						}
					}
					
					if(!flag){
						break;
					}
				}
				
				if(!flag){
					System.out.println("Set "+(counter++)+" is not immediately decodable");
				}else{
					System.out.println("Set "+(counter++)+" is immediately decodable");
				}
				
				list.clear();
			}else{
				list.add(temp);
			}
		}
	}
}


你可能感兴趣的:((Relax ST1.4)POJ 1056 IMMEDIATE DECODABILITY(判断一个字符串是否是另外一个字符串的前缀))