poj 2159 java
这是前几天做的,不算做完,只能算运行成功,因为运行时间挺久的,严重超时,要注意
1 import java.io.*; 2 import java.util.ArrayList; 3 import java.util.Collections; 4 import java.util.List; 5 6 public class Main{ 7 public static void main(String args[]){ 8 9 BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 10 11 String source,cipher; 12 char[] c1,c2; 13 boolean flag=true; 14 15 try{ 16 17 while((cipher=read.readLine())!=null){ 18 source = read.readLine(); 19 20 c1 = cipher.toCharArray(); 21 c2 = source.toCharArray(); 22 23 List<Integer> list1 = getList(c1); 24 List<Integer> list2 = getList(c2); 25 26 27 if(list1.size() != list2.size()){ 28 flag = false; 29 }else{ 30 31 for(int i=0;i<list1.size();i++){ 32 33 Collections.sort(list1); 34 Collections.sort(list2); 35 36 if(list1.get(i)!=list2.get(i)){ 37 flag = false; 38 break; 39 } 40 } 41 } 42 43 if(flag){ 44 System.out.println("YES"); 45 }else{ 46 System.out.println("NO"); 47 } 48 49 50 } 51 }catch(Exception e){ 52 e.printStackTrace(); 53 } 54 55 } 56 57 public static List<Integer> getList(char[] c){ 58 59 List<Integer> list = new ArrayList<Integer>(); 60 int index=0; 61 String used =""; //记录已经出现过的字母 62 63 for(int i=0;i<c.length;i++){ 64 if((index=used.indexOf(c[i]))==-1){ 65 66 used+=c[i]; 67 list.add(1); 68 69 }else{ 70 list.set(index, (Integer)list.get(index)+1); 71 } 72 } 73 74 return list; 75 } 76 }