| ||||||||||
| 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 | |||||||||
请问这道题几十毫秒是怎么做出来的?我的代码如下:至少需要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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator