| ||||||||||
| 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 | |||||||||
请问怎么才能让这个程序时间再缩短阿????!!!!!已经用了快速排序,还是TLE!!!谢谢了#include<iostream.h>
#include <stdlib.h>
#include<cstring>
struct state
{
char digit[15];
int num;
};
int main()
{
void change(char *a);
int mycompare(const void *elem1,const void *elem2);
int n;
cin>>n;
cin.ignore();
state *number=new state[n];
int i,j,count=0;
char tmp[20];
for(i=0;i<n;i++)
{
number[i].digit[0]='\0';
number[i].num=0;
}
for(i=0;i<n;i++)
{
cin.getline(tmp,20);
change(tmp);
for(j=0;j<count;j++)
{
if(strcmp(tmp,number[j].digit)==0)
{
number[j].num++;
break;
}
}
if(j==count)
{
strcpy(number[count].digit,tmp);
number[count].num++;
count++;
}
}
qsort(number,n,sizeof(state),mycompare);
for(i=0;i<n;i++)
{
if(number[i].num>1)
{
cout<<number[i].digit<<' '<<number[i].num<<endl;
}
}
delete []number;
return 0;
}
void change(char *a)
{
int i,j=0;
char tmp[9];
for(i=0;i<strlen(a);i++)
{
if(a[i]>='0'&&a[i]<='9')
{
tmp[j]=a[i];
j++;
}
else if(a[i]>='A'&&a[i]<='C')
{
tmp[j]='2';
j++;
}
else if(a[i]>='D'&&a[i]<='F')
{
tmp[j]='3';
j++;
}
else if(a[i]>='G'&&a[i]<='I')
{
tmp[j]='4';
j++;
}
else if(a[i]>='J'&&a[i]<='L')
{
tmp[j]='5';
j++;
}
else if(a[i]>='M'&&a[i]<='O')
{
tmp[j]='6';
j++;
}
else if(a[i]>='P'&&a[i]<='S'&&a[i]!='Q')
{
tmp[j]='7';
j++;
}
else if(a[i]>='T'&&a[i]<='V')
{
tmp[j]='8';
j++;
}
else if(a[i]>='W'&&a[i]<='Y')
{
tmp[j]='9';
j++;
}
}
tmp[7]=tmp[6];
tmp[6]=tmp[5];
tmp[5]=tmp[4];
tmp[4]=tmp[3];
tmp[3]='-';
tmp[8]='\0';
strcpy(a,tmp);
}
int mycompare(const void *elem1,const void *elem2)
{
state *p1,*p2;
p1=(state *)elem1;
p2=(state *)elem2;
return (strcmp(p1->digit,p2->digit));
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator