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

Re:TLE阿,问问各位怎么改进阿,GCC

Posted by slashdot at 2011-03-28 17:40:13 on Problem 1256
In Reply To:TLE阿,问问各位怎么改进阿,GCC Posted by:denganliang at 2011-03-28 17:31:21
> #include <stdio.h>
> #include <string.h>
> char s[15];
> int l;
> char str[52]={'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i',
> 'J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t',
> 'U','u','V','v','W','w','X','x','Y','y','Z','z'};
> void sort(){
> int a[15],len,i,j;
> char temp;
> for(i=0;i<15;i++)
>  a[i]=-1;
>  len=l;
> for(i=0;i<len;i++){
> 	for(j=0;j<52;j++){
> 	 if(s[i]==str[j]){
> 	   a[i]=j;
> 	   break;
> 	   }
> 	   }	   
> 	}
> for(i=0;i<len;i++)
>   for(j=i;j<len;j++){
> 	  if(a[i]>a[j])
> 	  {
> 		  temp=s[i];
> 		  s[i]=s[j];
> 		  s[j]=temp;
> 		  temp=a[i];
> 		  a[i]=a[j];
> 		  a[j]=temp;
> 		  }
> 	  }
> }
> void swap(char *a,char *b){
> 	char temp;
> 	temp=*a;
> 	*a=*b;
> 	*b=temp;
> 	}
> void pailie(int key,int len){
> 	int i;
> 	if(key==l-1) printf("%s\n",s);
> 	else{
> 		for(i=key;i<l;i++){
>             if(s[i]==s[i+1])
> 			{
> 				continue;
> 				}
> 			swap(&s[key],&s[i]);
> 			pailie(key+1,len-1);
> 			swap(&s[key],&s[i]);
> 			
> 			}
> 		}
> 	
> 	}
> 
> int main(){
> 	int k,n;
> 	scanf("%d",&n);
> 	for(k=0;k<n;k++){
> 		memset(s,'\0',15);
> 		scanf("%s",s);
> 		l=strlen(s);
> 		if(l==1) printf("%s\n",s);
> 		else{
> 		sort();
> 		
> 		//printf("\n%s\n",s);
>         pailie(0,l-1);
>         }
> 		}
> 	return 0;
> 	}

程序没有仔细看,用stl的next_permutation都不会超时。。。

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