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:为什么总是超时??求高手指点一下,谢谢

Posted by zoliu at 2012-09-14 04:11:06 on Problem 1002
In Reply To:为什么总是超时??求高手指点一下,谢谢 Posted by:zoliu at 2012-09-14 03:30:59
把convert提出来以减少递归的次数还是不行~~~~~~~~~~~
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void convert(char** ax,char** as,long size){
	
	
	long i,j,k;	
	char ch;
	for(k=0;k<size;k++){
		i=0,j=0;
		*(*(as+k)+3)='-';
		for(;(ch=*(*(ax+k)+i))!='\0';i++)
		{
			
			if(j==3) j++;
			if(ch=='-') {continue;}
			if(ch>='0'&&ch<='9') {as[k][j++]=ch;continue;}
			if(ch<=67) {as[k][j++]='2';continue;}
			if(ch<=70) {as[k][j++]='3';continue;}
			if(ch<=73) {as[k][j++]='4';continue;}
			if(ch<='L'){as[k][j++]='5';continue;}
			if(ch<='O'){as[k][j++]='6';continue;}
			if(ch<='S'){as[k][j++]='7';continue;}
			if(ch<='V'){as[k][j++]='8';continue;}
			if(ch<='Y'){as[k][j++]='9';continue;}
		}
		as[k][j++]='\0';
	//	puts(as[k]);
	}
	//return as;
}

void sort(char** ass,int size)
{
	int i,j;
	char* temp;
	for(i=1;i<size;i++){
		temp = ass[i];
		j=i-1;
		while(j>=0&&strcmp(ass[j],temp)>0){
			ass[j+1]=ass[j];
			ass[j]=temp;
			j--;
		}
		
	}
	
}
int main(void)
{
	
	long caseNum;
	long i=0;
	int count=1;
	char **arr,**ass;
	
	scanf("%ld",&caseNum);
	//初始化指针数组
	arr = (char**)malloc(sizeof(char*)*caseNum);
	ass = (char**)malloc(sizeof(char*)*caseNum);

	for(;i<caseNum;i++){
		arr[i] = (char*)malloc(sizeof(char)*15);
		ass[i] = (char*)malloc(sizeof(char)*8);
		scanf("%s",arr[i]);
		//convert(arr[i],ass[i]);
		
	}
	convert(arr,ass,caseNum);
	sort(ass,caseNum);

	for(i=0;i<caseNum-1;i++){
		if(strcmp(ass[i],ass[i+1])==0){
			count++;continue;
		}else{
			if(count>1){
				printf("%s %d\n",ass[i],count);
				count=1;
			}	
			continue;
		}
	}

	return EXIT_SUCCESS;
	
}


> 代码如下:
> 
> #include<stdio.h>
> #include<stdlib.h>
> #include<string.h>
> #include<time.h>
> 
> char* convert(char* ax,char* as){
> 	int length=strlen(ax);
> 	int i=0,j=0;	
> 	as[3]='-';
> 	for(;*(ax+i)!='\0';i++)
> 	{
> 		
> 		if(j==3) j++;
> 		if(ax[i]=='-') {continue;}
> 		if(ax[i]>='0'&&ax[i]<='9') {as[j++]=ax[i];continue;}
> 		if(ax[i]<=67) {as[j++]='2';continue;}
> 		if(ax[i]<=70) {as[j++]='3';continue;}
> 		if(ax[i]<=73) {as[j++]='4';continue;}
> 		if(ax[i]<='L'){as[j++]='5';continue;}
> 		if(ax[i]<='O'){as[j++]='6';continue;}
> 		if(ax[i]<='S'){as[j++]='7';continue;}
> 		if(ax[i]<='V'){as[j++]='8';continue;}
> 		if(ax[i]<='Y'){as[j++]='9';continue;}
> 	}
> 	*(as+j)='\0';
> 
> 	return as;
> }
> 
> void sort(char** ass,int size)
> {
> 	int i,j;
> 	char* temp;
> 	for(i=1;i<size;i++){
> 		temp = ass[i];
> 		j=i-1;
> 		while(j>=0&&strcmp(ass[j],temp)>0){
> 			ass[j+1]=ass[j];
> 			ass[j]=temp;
> 			j--;
> 		}
> 		
> 	}
> 	
> }
> int main(void)
> {
> 	
> 	long caseNum;
> 	long i=0;
> 	int count=1;
> 	char **arr,**ass;
> 	
> 	scanf("%ld",&caseNum);
> 	//初始化指针数组
> 	arr = (char**)malloc(sizeof(char*)*caseNum);
> 	ass = (char**)malloc(sizeof(char*)*caseNum);
> 
> 	for(;i<caseNum;i++){
> 		arr[i] = (char*)malloc(sizeof(char)*15);
> 		ass[i] = (char*)malloc(sizeof(char)*8);
> 		scanf("%s",arr[i]);
> 		convert(arr[i],ass[i]);
> 		
> 	}
> 	
> 	sort(ass,caseNum);
> 
> 	for(i=0;i<caseNum-1;i++){
> 		if(strcmp(ass[i],ass[i+1])==0){
> 			count++;continue;
> 		}else{
> 			if(count>1){
> 				printf("%s %d\n",ass[i],count);
> 				count=1;
> 			}	
> 			continue;
> 		}
> 	}
> 
> 	return EXIT_SUCCESS;
> 	
> }

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