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

请问这道题几十毫秒是怎么做出来的?

Posted by purplefly at 2006-07-29 20:29:32 on Problem 2681
我的代码如下:至少需要200多ms,
#include<stdio.h>
#include<string.h>
#include<math.h>

int max(int a,int b)
{
	if(a>=b) return a;
	return b;
}

int min(int a,int b)
{
	if(a<=b) return a;
	return b;
}

void main()
{   
	int i,k,m,n,distance,temp1,temp2,t,flag,hash[26];
	char ch,space,a[2][48],refer[26];

	scanf("%d",&n);
	space=getchar();
	m=1;

	while(n--)
	{
		
        gets(a[0]);
	
        gets(a[1]);
        
		if(a[1][0]=='\0') {printf("Case #%d:  %d\n",m++,strlen(a[0]));continue;}
		
        if(a[0][0]=='\0') {printf("Case #%d:  %d\n",m++,strlen(a[1]));continue;}
	
		distance=0;

		for(i=0;i<26;i++)
			hash[i]=0;
        
		k=0;
        t=0;

		while(k<(signed)strlen(a[0]))
		{
			ch=a[0][k];
            if(hash[ch-97]==1) {k++;continue;}
			hash[ch-97]=1;
        /*    
			flag=0;
			for(i=0;i<t;i++)
				if(refer[i]==ch) {flag=1;break;}

			if(flag==0) refer[t++]=ch;
			else {k++;continue;}
*/
            temp1=0;
		    temp2=0;

		for(i=0;i<max((signed)strlen(a[0]),(signed)strlen(a[1]));i++)
		{
			if(a[0][i]==ch&&i<(signed)strlen(a[0])) temp1++;
            
			if(a[1][i]==ch&&i<(signed)strlen(a[1])) temp2++;
		}
		
  
		distance+=min(temp1,temp2);
		k++;
		}

		flag=2*distance;
		distance=strlen(a[0])+strlen(a[1])-flag;
		printf("Case #%d:  %d\n",m++,distance);
	}
}

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