Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用java写的,哪位高手能帮我改改,别让它超时了。。万分感谢

Posted by jucai at 2007-05-08 20:35:40 on Problem 1256
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator