Phone List HDU - 1671(字典树模板题)

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

Input The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits. Output For each test case, output “YES” if the list is consistent, or “NO” otherwise. Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES
题意:给你几串字符串,要你判断是否存在某串字符串是另一串字符串的前缀,如果有输出NO,否则YES

思路:如果想要边插入边判断的话,我们就要开两个标记数组才能达到目的,一个标记该层次这个字符是否出现过,另一个标记该层次这个字符是否是单词的结尾。详细请看代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=1e9+7;
const double pi=acos(-1);
const double eps=1e-8;
char s[12];
int trie[N][10],tot;
bool vis[N],flag,vis1[N];//vis标记字符是否出现过,vis1标记字符是否是单词的结尾
void sert()
{
    int root=0;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int id=s[i]-'0';
        if(!trie[root][id]) trie[root][id]=++tot;
        root=trie[root][id];
        if(vis[root]&&vis1[root]) flag=false;//如果这个字符出现过并且还是单词的结尾,说明存在前缀
        if(i!=len-1) vis[root]=true;
    }
    if(!vis[root]) vis[root]=true;
    else flag=false;//如果最后一个字符出现过,那么说明这个单词是一个前缀
    vis1[root]=true;//标记这是一个单词的结尾
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        flag=true,tot=0;
        memset(trie,0,sizeof(trie));
        memset(vis,false,sizeof(vis));
        memset(vis1,false,sizeof(vis1));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            sert();
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}

/*
3
3
911
97625999
91125426
5
113
12340
123440
12345
98346
3
97625999
91125426
911


NO
YES
NO
*/
当然了还有另外的方法,网上很多思路都是将所有字符串存起来然后再按字典序排序,再插入到字典树中,这样就可以开一个vis标记数组了,只需标记字符是否出现过,只要出现过就说明存在前缀。但是这里我并没有按字典序来排,我是按照字符串长度来排序的,短的在前面,长的在后面,可以达到同样的效果
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=1e9+7;
const double pi=acos(-1);
const double eps=1e-8;
int trie[N][12],tot;
bool vis[N],flag;
void sert(char s[])
{
    int root=0;
    for(int i=0;s[i]!='\0';i++)
    {
        int id=s[i]-'0';
        if(!trie[root][id]) trie[root][id]=++tot;
        root=trie[root][id];
        if(vis[root]) flag=false;
    }
    if(!vis[root]) vis[root]=true;
}
struct node
{
    char ss[12];
    int len;
}s[N];
bool cmp(node x,node y)
{
    return x.len<y.len;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        flag=true,tot=0;
        memset(trie,0,sizeof(trie));
        memset(vis,false,sizeof(vis));
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i].ss);
            s[i].len=strlen(s[i].ss);
        }
        sort(s,s+n,cmp);
        for(int i=0;i<n;i++) sert(s[i].ss);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
}

阅读更多
文章标签: 字典树
个人分类: hdu 字典树
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Phone List HDU - 1671(字典树模板题)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭