HDU 1671 Phone List 字典树 多种做法:问你字典中是否有一个字符串是其他字符串的前缀?

Phone List

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 26141    Accepted Submission(s): 8724

 

Problem Description

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

 

 

Source

2008 “Insigma International Cup” Zhejiang Collegiate Programming Contest - Warm Up(3)

 

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  1075 1247 1800 1677 1298 

 

 

Statistic | Submit | Discuss | Note

算法分析

一开始按照统计思路做的(HDU 1251 统计难题 字典树简单应用),但是超时,

第二种思路:想着标记末尾单词,如果达到末尾单词,就NO,但就一直wa,看别人代码发现忽略了一种情况,就是先输入比较长的字符串,再输入较短的字符串,如果是这种情况,那么在这个过程中如果没有新节点说明较短的字符串就是长的字符串的一个前缀

其实也可以排序在做。

又发现第一种思路是可以的

HDU 1251 统计难题 字典树简单应用,第二种思路先做一下这题,根据上题的定义,val[u]表示在u结点表示末尾单词前缀出现的次数,那我们这么想,全部插入,然后查询如果val[u]>=2(因为有本身所以大于2),就ok。

 

代码实现

第一种思路:

#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 300000+5
#define MAX 26
 
typedef long long ll;
 
const int maxnode=100000+100;//预计字典树最大节点数目
const int sigma_size=11;       //每个节点的最多儿子数
 
struct Trie
{
    int ch[maxnode][sigma_size];//ch[i][j]==k表示第i个节点的第j个儿子是节点k
    int val[maxnode];//val[i]==x表示第i个节点的权值为x
    int sz;//字典树一共有sz个节点,从0到sz-1标号
 
    //初始化
    void clear()
    {
        sz=1;
        memset(ch,0,sizeof(ch));//ch值为0表示没有儿子
        memset(val,0,sizeof(val));
    }
 
    //在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
    //所以insert前需要判断一下是否已经存在s单词了
    void insert(string s)
    { 
		int u=0,n=s.length();
        for(int i=0;i<n;i++)///建立字典树
        {
        	
            int id=s[i]-'0'; 
            if(ch[u][id]==0)//无该儿子
            {
                ch[u][id]=sz++;
                memset(ch[sz],0,sizeof(ch[sz]));
            }
            u=ch[u][id];
            val[u]++;//标记到目前为止的这个前缀出现的次数 
        }
       
    }
 
    //在字典树中查找单词s
    int find(string s)
    {
        int n=s.length(),u=0;
        for(int i=0;i<n;i++)
        {
            int id=s[i]-'0';
			u=ch[u][id];
//cout<<"*"<<id<<" "<<val[u]<<" "<<u<<endl;
        }
         if(val[u]>=2) return 1;
         return 0;
    }
};
 string s[N];
Trie trie;
int main()
{
 
    int TT;
    cin>>TT;
    int n,m;
    while(TT--)
    {
    	trie.clear();
    	scanf("%d",&n);
    	cin.get();
    	int flag=1;
    	
        for(int i=0;i<n;i++)
		{   
		   cin>>s[i];
		 
		  trie.insert(s[i]);
		}
		for(int i=0;i<n;i++)
		{
		 
		  if(trie.find(s[i]))
		  {
		  	flag=0;
		  	break;
		  } 
		}
		if(flag==0)
			printf("NO\n");
		else
			printf("YES\n");
        
        
    }
    return 0;
}

第一种思路的另一种想法:

#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
#define N 300000+5
#define MAX 26
 
typedef long long ll;
 
const int maxnode=100000+100;//预计字典树最大节点数目
const int sigma_size=11;       //每个节点的最多儿子数
 
struct Trie
{
    int ch[maxnode][sigma_size];//ch[i][j]==k表示第i个节点的第j个儿子是节点k
    int val[maxnode];//val[i]==x表示第i个节点的权值为x
    int sz;//字典树一共有sz个节点,从0到sz-1标号
 
    //初始化
    void clear()
    {
        sz=1;
        memset(ch,0,sizeof(ch));//ch值为0表示没有儿子
        memset(val,0,sizeof(val));
    }
 
    //在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
    //所以insert前需要判断一下是否已经存在s单词了
    void insert(string s)
    { 
		int u=0,n=s.length();
        for(int i=0;i<n;i++)///建立字典树
        {
        	
            int id=s[i]-'0'; 
            if(ch[u][id]==0)//无该儿子
            {
                ch[u][id]=sz++;
                memset(ch[sz],0,sizeof(ch[sz]));
            }
            u=ch[u][id];
            val[u]++;//标记到目前为止的这个前缀出现的次数 
        }
       
    }
 
    //在字典树中查找单词s
    int find(string s)
    {
        int n=s.length(),u=0;
        for(int i=0;i<n;i++)
        {
            int id=s[i]-'0';
			u=ch[u][id];
//cout<<"*"<<id<<" "<<val[u]<<" "<<u<<endl;
			if(val[u]==1) return 0;
			
            //if(val[u]>=2) return 0;
        }
        return 1;
    }
};
 string s[N];
Trie trie;
int main()
{
 
    int TT;
    cin>>TT;
    int n,m;
    while(TT--)
    {
    	trie.clear();
    	scanf("%d",&n);
    	cin.get();
    	int flag=1;
    	
        for(int i=0;i<n;i++)
		{   
		   cin>>s[i];
		 
		  trie.insert(s[i]);
		}
		for(int i=0;i<n;i++)
		{
		 
		  if(trie.find(s[i]))
		  {
		  	flag=0;
		  	break;
		  } 
		}
		if(flag==0)
			printf("NO\n");
		else
			printf("YES\n");
        
        
    }
    return 0;
}

第二种思路:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define MAX 26
const int maxnode=4000*100+100;//预计字典树最大节点数目
const int sigma_size=26;//每个节点的最多儿子数
struct Trie
{
    int ch[maxnode][sigma_size];//ch[i][j]==k表示第i个节点的第j个儿子是节点k
    int val[maxnode];//val[i]==x表示第i个节点的权值为x
    int sz;//字典树一共有sz个节点,从0到sz-1标号
 
    //初始化
    void clear()
    {
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));//ch值为0表示没有儿子
        memset(val,0,sizeof(val));
    }
 
    //在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
    //所以insert前需要判断一下是否已经存在s单词了
    int insert(string s)
    { 
        int u=0,n=s.length();
        bool flag=0;
        for(int i=0;i<n;i++)///建立字典树
        {
            int id=s[i]-'0';
            if(ch[u][id]==0)//无该儿子
            {
                ch[u][id]=sz;  
                flag=1;
                memset(ch[sz],0,sizeof(ch[sz]));
                sz++;
            }
            u=ch[u][id];
            if(val[u]==1)
            {
                return 0;
            }
        }  
        val[u]=1;
        return flag;
    }
 
    //在字典树中查找单词s
    bool find(string s)
    {
        int n=s.length(),u=0;
        for(int i=0;i<n;i++)
        {
            int id=s[i]-'0';
            if(ch[u][id]==0)
                return false;
            u=ch[u][id];
        }
        return val[u];
    }
};
Trie trie;
int main()
{
 
    int TT;
    cin>>TT;
    int n,m;
    while(TT--)
    {
        trie.clear();
        scanf("%d",&n);
        cin.get();
        int flag=1;
        for(int i=0;i<n;i++)
        {
           string s;
           cin>>s;
           if(flag==1) 
           {
               flag=trie.insert(s);
           }
        }
        
        if(flag==0)
            printf("NO\n");
        else
            printf("YES\n");
        
        
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值