/* * 分析:题意要求找字符串个数最多的一个 * 方法:很多,hash就是其中一种 * 注意两点:1.去掉前导0; 2.每组测试数据后,要释放内存 * 第一点解析:例如数据01,001,0001, 00001不去前导0的话,hash以后映射到不同的表位置 */ /* Author: Try86 Date: 09/04/12 18:01 */ #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int M = 5471; //hash表大小 struct node { //节点 char str[31]; int num; node *next; node(char *ch, int n) {//构造 strcpy(str, ch); num = n; } }; struct hashTable {//hash表 node *link; }hash[M]; int maxNum; char ans[31], str[31]; void init() {//初始化 for (int i=0; i<M; ++i) hash[i].link = NULL; return ; } unsigned int BKDRHash(char *str) {//hash unsigned seed = 131; unsigned hash = 0; while (*str) hash = hash *seed + (*str++); return hash & 0x7FFFFFFF; } void insertAndFind(char *str) {//插入同时查找 int k = BKDRHash(str) % M; node *p = hash[k].link; while (p) { if (!strcmp(p->str, str)) { ++p->num; if (p->num >maxNum) { maxNum = p->num; strcpy(ans, p->str); return ; } } p = p->next; } node *q = new node(str, 1); q->next = hash[k].link; hash[k].link = q; return ; } void del(node *p) {//释放内存,不然超内存 if (!p) return ; del(p->next); delete p; } int main(){ int n; while (scanf("%d", &n) != EOF) { init(); maxNum = 1; for (int i=0; i<n; ++i) { scanf ("%s", str); int j; for (j=0; str[j]=='0'; ++j);//去掉前导0 insertAndFind(str+j); } for (int i=0; i<M; ++i) del(hash[i].link); printf ("%d\n", maxNum); } return 0; }