
4 10 20 30 04 5 2 3 4 3 4
1 2
题意:求数列中出现最多的数的个数,但是需要之一的是熟的长度有30,以前居然int水过,现在用字典树写了一遍
需要注意的是要删除内存,不然会超的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define eps 1e-8
typedef __int64 ll;
#define fre(i,a,b) for(i = a; i < b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define ssf(n) scanf("%s", n)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define bug pf("Hi\n")
using namespace std;
#define INF 0x3f3f3f3f
#define N 10
struct stud{
int num;
stud * next[N];
//void he(){num=0;for(int i=0;i<N;i++) next[i]=NULL;}
}*root;
int ans;
void insert(char *c)
{
stud *p=root;
while(*c)
{
if(p->next[*c-'0']==NULL)
{
stud *t=(stud *)malloc(sizeof(stud));
for(int i=0;i<N;i++)
t->next[i]=NULL;
t->num=0;
p->next[*c-'0']=t;
p=t;
}
else p=p->next[*c-'0'];
c++;
}
p->num++;
ans=max(p->num,ans);
}
void delet(stud* temp)
{
int i;
if(temp==NULL) return ;
for(i=0;i<N;i++)
if(temp->next[i]!=NULL)
delet(temp->next[i]);
delete(temp);
}
int main()
{
int i,j;
char ch[35];
int n;
while(~scanf("%d",&n))
{
root=(stud *)malloc(sizeof(stud));
for(i=0;i<N;i++)
root->next[i]=NULL;
root->num=0;
ans=0;
while(n--)
{
scanf("%s",ch);
for(i=0;ch[i];i++)
if(ch[i]!='0') break;
insert(ch+i);
}
printf("%d\n",ans);
delet(root);
}
return 0;
}
HDU 1800 Flying to the Mars(字典树)
原文:http://blog.csdn.net/u014737310/article/details/45587643