hdu 1800 Flying to the Mars
//法1 map 640MS
#include <cstdio>
#include <string>
#include <map>
using namespace std;
map<string,int> level;
string str;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
level.clear();
int i,j=n,t,max=0;
char temp[32]={0};
while(j--)
{
scanf("%s",temp);
for(i=0;temp[i] && temp[i]=='0';i++);
if(i)strcpy(temp,temp+i);
str.assign(temp);
t=++level[str];
if(t>max)max=t;
}
printf("%d\n",max);
}
return 0;
}
//法2 字典树 218MS
#include <stdio.h>
#include <cstdlib>
struct trieNode
{
trieNode()
{
int i;
for(i=0;i<10;i++)
next[i]=NULL;
value=0;
}
trieNode *next[10];
int value;
};
trieNode *root=NULL;
int Max;
void InsertNode(char * word)
{
if(root==NULL)root=new trieNode;
trieNode *p=root;
trieNode *temp;
bool flag=false;
int t;
while(*word)
{
t=*word-'0';
if(t!=0)
flag=true;
if(flag)
{
if(p->next[t]==NULL)
{
temp=new trieNode;
p->next[t]=temp;
p=p->next[t];
}
else
{
p=p->next[t];
}
}
word++;
}
p->value++;
}
void deltirNode(trieNode *p)
{
if(p==NULL)
return;
if(p->value>Max)
Max=p->value;
int i;
for(i=0;i<10;i++)
if(p->next[i])
deltirNode(p->next[i]);
free(p);
}
int main()
{
int n;
char str[30];
while(scanf("%d",&n)!=EOF)
{
getchar();
root=NULL;
Max=0;
while(n--)
{
scanf("%s",str);
InsertNode(str);
}
deltirNode(root);
printf("%d\n",Max);
}
return 0;
}
//法3 别人的 hash
#include <stdio.h>
#include <memory.h>
#define MAXN 10000
inline int ELFhash(char *key)
{
unsigned long h = 0;
unsigned long g;
while( *key )
{
h =( h<< 4) + *key++;
g = h & 0xf0000000L;
if( g ) h ^= g >> 24;
h &= ~g;
}
return h;
}
int hash[MAXN],count[MAXN];
int maxit,n;
inline void hashit(char *str)
{
int k,t;
while( *str == '0' ) str++;
k = ELFhash(str);
t = k % MAXN;
while( hash[t] != k && hash[t] != -1 )
t = ( t + 5 ) % MAXN;
if( hash[t] == -1 )
count[t] = 1,hash[t] = k;
else if( ++count[t] > maxit )
maxit = count[t];
}
int main()
{
char str[100];
while(scanf("%d",&n)!=EOF)
{
memset(hash,-1,sizeof(hash));
for(maxit=1,gets(str);n>0;n--)
{
gets(str);
hashit(str);
}
printf("%d\n",maxit);
}
}
浙公网安备 33010602011771号