【题目】http://acm.hdu.edu.cn/showproblem.php?pid=1004
【报告】
按照题目的意思,就是找出出现次数最多的字符串。
很轻松,可以先排序一下,然后一个个统计,也可以直接HASH。我就是用HASH的。HASH函数的构造可以直接用线性探测再散列,相对来说还是容易的。
【程序】
// TASK: 1004 Let the Balloon Rise
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <string>
#define N 1000
using namespace std;
string s,ans;
long n,ams;
long h[N+1];
string ha[N+1];
inline void hash(string s)
{
long q=0;
for (long i=0;i<s.length();i++)
q=(q+s[i])%N;
while (h[q]>0&&ha[q]!=s) q=(q+1)%N; // 线性探测再散列
ha[q]=s;
h[q]++;
if (ams<h[q])
{
ans=s;ams=h[q];
}
}
int main(int argc, char *argv[])
{
while (cin >> n)
{
if (n<=0) break;
ans="";ams=0;
memset(h,0,sizeof(h));
for (long i=1;i<=n;i++)
{
cin >> s;
hash(s);
}
cout << ans << endl;
}
return EXIT_SUCCESS;
}