Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用java写的,哪位高手能帮我改改,别让它超时了。。万分感谢import java.util.*; public class Anagram1 { static char[] ss; static String sa[] ; static int m; public static void main(String [] args) { Scanner cin = new Scanner(System.in); int t = cin.nextInt(); String line = cin.nextLine(); while(t != 0){ String s = cin.nextLine(); ss = s.toCharArray(); sa = new String[100000]; m = 0; doAnagram(s.length()); for(int i = 0; i < m; i ++){ for(int k = i; k < m; k ++){ if(sa[k].compareTo(sa[i]) < 0){ String temp = sa[i]; sa[i] = sa[k]; sa[k] = temp; } } System.out.println(sa[i]); } t --; } } public static void doAnagram(int n) { if(n == 1) return; for(int j = 0; j < n; j ++){ doAnagram(n-1); if(n == 2){ int i; String s1 = String.copyValueOf(ss); for(i = 0; i < m; i ++){ if(sa[i].equals(s1)) break; } if(i == m){; sa[m] = s1; m ++; } } rotate(n); } } public static void rotate(int n) { int j; int p = ss.length - n; char temp = ss[p]; for(j = p+1; j < ss.length; j ++) ss[j-1] = ss[j]; ss[j-1] = temp; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator