主题:[讨论]2104这道题怎么总是超时啊,代码如下
http://acm.zju.edu.cn/show_problem.php?pid=2104
#include <iostream>
#include <string>
using namespace std;
int cmp(const void*p1, const void*p2)
{
return strcmp((char*)p1,(char*)p2);
}
main()
{
char s[1001][30];
char t[30],max[30],mark;
int n,i,sum;
while (cin>>n&&n)
{
for(i=0;i<n;i++)
cin>>s[i];
qsort(s,n,sizeof(s[0]),cmp);
i=0,mark=-1;
while(n>0) {
sum=0;
strcpy(t,s[i]);
while(!strcmp(t,s[i]))
sum++,i++;
if(sum>mark)
{
strcpy(max,t);
mark=sum;
}
n-=i;
}
cout << max << endl;
}
}
#include <iostream>
#include <string>
using namespace std;
int cmp(const void*p1, const void*p2)
{
return strcmp((char*)p1,(char*)p2);
}
main()
{
char s[1001][30];
char t[30],max[30],mark;
int n,i,sum;
while (cin>>n&&n)
{
for(i=0;i<n;i++)
cin>>s[i];
qsort(s,n,sizeof(s[0]),cmp);
i=0,mark=-1;
while(n>0) {
sum=0;
strcpy(t,s[i]);
while(!strcmp(t,s[i]))
sum++,i++;
if(sum>mark)
{
strcpy(max,t);
mark=sum;
}
n-=i;
}
cout << max << endl;
}
}