码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 11362 - Phone List

时间:2017-05-14 19:36:22      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:define   tno   class   art   trie   next   phone   std   利用   

题目:给你一组电话号码,推断是否有一些号码是其它的前缀(或相等)。

分析:字符串。字典树。利用字典树储存查询就可以,注意两种情况处理:

            1.先短后长(前缀在前);2.先长后短(前缀在后)。

说明:第580题了,目标600╮(╯▽╰)╭。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

#define nodesize 100001     //节点个数     
#define dictsize 128        //字符集大小     

//trie
typedef struct node1  
{    
    int    flag;            //值域     
    node1* next[dictsize];    
}tnode;    
tnode dict[nodesize];  
  
class Trie    
{    
    private:  
        int    size;
        tnode* root;    
    public:    
        Trie() {initial();}    
        void initial() {    
            memset(dict, 0, sizeof(dict));    
            size = 0;  	 root = newnode();    
        }
        tnode* newnode() {return &dict[size ++];}    
        int insert(char* word) {    
            tnode* now = root;    
            for (int i = 0 ; word[i] ; ++ i) {  
                if (!now->next[word[i]])    
                    now->next[word[i]] = newnode(); 
                now = now->next[word[i]];
                if (now->flag) return 1;
            }now->flag = 1;
            for (int i = 0 ; i < dictsize ; ++ i)
            	if (now->next[i])
            		return 1;
            return 0;
        }
}trie;    
//trie end

int main()
{
	int t,n;
 	char buf[12];
	while (~scanf("%d",&t))
	while (t --) {
		trie.initial();
		scanf("%d",&n);
		int flag = 0;
		for (int i = 0 ; i < n ; ++ i) {
			scanf("%s",buf);
			if (trie.insert(buf))
				flag = 1;
		}
	
		if (flag)
			printf("NO\n");
		else printf("YES\n");
	} 
    return 0;
}


UVa 11362 - Phone List

标签:define   tno   class   art   trie   next   phone   std   利用   

原文地址:http://www.cnblogs.com/gavanwanggw/p/6853393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!