很水的一个题目,甚至开始我的代码是错的都能过。题目先用hash过掉了,然后在学者用了一下map,stl这个暑假要学一下,真的好有用。
#include<iostream>
#include<string>
#include<map>

using namespace std;


int main()
{
map<string, int> m;
int indexx = 0, i, cnt[1001], n, max, temp, ti;
char ch[20], save[1001][20];

while(scanf("%d",&n), n != 0)
{
m.clear();indexx=0;

for(i=0; i<n; i++)
{
scanf("%s",ch);
temp = m[ch];

if(temp == 0)
{
// temp = indexx++;
temp = ++indexx;
m[ch] = temp;
strcpy(save[temp], ch);
}
cnt[temp]++;
}
max = -1;
for(i=1; i<=indexx; i++)

if(max < cnt[i])
{
max = cnt[i];
ti = i;
}
printf("%s\n",save[ti]);
memset(cnt, 0, sizeof(cnt));
}
system("pause");
return 0;
}

开始错误的写法是注释掉的那种,那样有错误我觉得。indexx的作用就像trie里面或者是静态邻接表里面的作用一样。#include<string.h>会ce,不知为何。