hdu 1800 Flying to the Mars

http://acm.hdu.edu.cn/showproblem.php?pid=1800

这个题,就是在插入的时候注意一下前导零的问题,其他的就没什么了,祝君ac。

上代码:

View Code
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

const int maxn=10;

struct node
{
    int cnt;
    node *next[maxn];
    node()
    {
        cnt=0;
        for(int i=0;i<maxn;i++)
            next[i]=NULL;
    }
    ~node()
    {
        for(int i=0;i<maxn;i++)
        {
            if(next[i]!=NULL)
                next[i]=NULL;
        }
    }
};

inline int max(int a,int b)
{
    return a<b?b:a;
}

int n,ans;
class Trie
{
    public:
    node *root;
    Trie()
    {
        root=NULL;
    }
    void insert(char *s)
    {
        if(!root)
            root=new node();
        int len=strlen(s);
        node *loca=root;
        for(int i=0;i<len;i++)
        {
            int id=s[i]-'0';
            if(loca->next[id]==NULL)
                loca->next[id]=new node();
            loca=loca->next[id];
        }
        loca->cnt++;
        ans=max(loca->cnt,ans);
//        printf("%s: %d\n",s,ans);
    }
};

int main()
{
    char s[31];
    while(~scanf("%d",&n))
    {
        Trie t;
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            int index=0;
            while(s[index]=='0') index++;
//            cout<<s+index<<endl;
            t.insert(s+index);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 善待每一天,努力做好自己。

欢迎转载,注明出处。

原文地址:https://www.cnblogs.com/RainingDays/p/3068746.html