hdu 1671 Phone List

这题的题解随处可见。仅供参考。

ac代码:

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

using namespace std;

const int maxn=10;
const int maxlen=11;

typedef struct TrieNode
{
    int flg;
    struct TrieNode *next[maxn];
}TrieNode;

TrieNode memory[1000000];
int allop=0,nflg=0;

void InitTrieRoot(TrieNode **pRoot)
{
    *pRoot=NULL;
}

TrieNode *CreatTrieNode()
{
    TrieNode *p;
    p=&memory[allop++];
    p->flg=0;
    for(int i=0;i<maxn;i++)
        p->next[i]=NULL;
    return p;
}

void InsertTrieNode(TrieNode **pRoot,char *s)
{
    TrieNode *p;
    if(!(p=*pRoot))
        p=*pRoot=CreatTrieNode();
    int i=0;
    while(s[i])
    {
        int k=s[i++]-'0';
        if(p->next[k])
        {
            if(p->next[k]->flg==1||s[i]=='\0')
            {
                nflg=1;
                return;
            }
        }
        else
            p->next[k]=CreatTrieNode();
        p=p->next[k];
    }
    p->flg=1;
}
int main()
{
    TrieNode *root;
    int ncas,n;
    char ss[maxlen];
    scanf("%d",&ncas);
    while(ncas--)
    {
        nflg=allop=0;
        InitTrieRoot(&root);
        scanf("%d",&n);
        getchar();
        for(int tt=1;tt<=n;tt++)
        {
            gets(ss);
            if(!nflg)
                InsertTrieNode(&root,ss);
        }
        nflg?printf("NO\n"):printf("YES\n");
    }
    return 0;
}

只是这题不晓得为什么用我常用的那个字典树的模板就过不掉。郁闷。

转载于:https://www.cnblogs.com/RainingDays/archive/2012/11/12/2766569.html

猜你喜欢

转载自blog.csdn.net/weixin_33758863/article/details/94538703