[字典树] hdu 1800 fly to the mar

来源:互联网 发布:java jdbcutils 编辑:程序博客网 时间:2024/05/23 13:45
/**[字典树] hdu 1800 fly to the mar开始以为求出最多有多少个单调序列,这个真不会,发现是求众数。先map了一下,直接tle了。然后字典树,注意1,去掉前导0;2,如果是0,要保留一个。*/#include <stdio.h>#include <string>#include <string.h>#include <iostream>#include <map>#include <algorithm>using namespace std;#define N 3000#define MAXN 100000struct tireTree{    int cnt;    tireTree *child[10];}tt[MAXN],*spt;void insert(char *s,tireTree *&rt){    int num,i;    tireTree *loc;    loc = rt;    for(i = 0; s[i]; ++i)    {        num = s[i] - '0';        if(loc -> child[num] == NULL)            loc -> child[num] = spt++;        loc = loc -> child[num];    }    loc -> cnt ++;}int maxx;void dfs(tireTree *rt){    int i;    for(i = 0; i < 10; ++i)    {        if(rt -> child[i])        {            maxx = max(maxx,rt -> child[i] -> cnt);            dfs(rt -> child[i]);        }    }}int main(){    int n;    char str[33],*ptr;    while(scanf("%d",&n) == 1)    {        tireTree *rt;        memset(tt,0,sizeof(tt));        spt = tt;        rt = spt++;        while(n--)        {            scanf("%s",str);            ptr = str;            while(*ptr == '0')                ++ptr;            if(*ptr == 0)                --ptr;            insert(ptr,rt);        }        maxx = 0;        dfs(rt);        printf("%d\n",maxx);    }    return 0;}/**int main(){    int n,i,res;    char str[33],*pt;    string lev[N];    map<string,int> mp;    while(scanf("%d",&n) == 1)    {        for(i = 0; i < n; ++i)        {            scanf("%s",str);            pt = str;            while(*pt == '0')                pt++;            lev[i] = string(pt);            mp[lev[i]]++;        }        res = 0;        for(i = 0; i < n; ++i)            res = max(res,mp[lev[i]]);        printf("%d\n",res);        mp.clear();    }    return 0;}*/

原创粉丝点击