zoukankan      html  css  js  c++  java
  • poj_3630Phone List

    Phone List
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18645   Accepted: 5910

    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:

    • Emergency 911
    • Alice 97 625 999
    • 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
    
    
    
    
    #include<iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 100000;
    const int MAX = 10;
    
    typedef struct Node
    {
    	bool isstr;
    	Node *next[MAX];
    }Node, *Trie;
    Node node[MAXN];
    Trie root;
    int cnt;
    bool flag;
    
    void insert(char *str)
    {
    	bool new_flag = false;
    	Trie current = root;
    	int length = strlen(str);
    	for (int i = 0; i < length; i++)
    	{
    		if(current->isstr == true)  // 前面的串为str的前缀。
    		{
    			flag = false;
    			return;
    		}
    		int id = str[i] - '0';
    		if(current->next[id] == NULL)
    		{
    			new_flag = true;             //  有新的节点开辟,证明str不为前面的串的前缀。
    			node[cnt].isstr = false;    //   新节点的初始化。
    			for (int i = 0; i < MAX; i++)
    			{
    				node[cnt].next[i] = NULL;
    			}  
    			current->next[id] = &node[cnt++];
    		}
    		current = current->next[id];
    	}
    	current->isstr = true;
    	if(!new_flag) flag = false;          // str为前面的串的前缀。
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int t, n;
    	char word[15] = {0};
    	scanf("%d", &t);
    	while(t --)
    	{
    		scanf("%d", &n);
    		cnt = 0;
    		flag = true; //是前面的串的前缀时,flag为false
    		root = (Trie)malloc(sizeof(Node));
    		for (int i = 0; i < MAX; i++)
    		{
    			root->next[i] = NULL;
    		}
    		while(n--)
    		{
    			scanf("%s", word);
    			if(flag)
    			{
    				insert(word);
    			}
    		}
    		if(flag) printf("YES\n");
    		else printf("NO\n");
    	}
    	return 0;
    }


  • 相关阅读:
    clickhouse数据文件目录移动到新目录并建立软连接
    时隔七年,来一篇
    微信支付v3版本PHP v3/merchant/media/upload 图片上传
    laravel项目在lnmp环境上线出现404错误
    Linux下sysstat工具学习
    完美快速解决百度分享不支持HTTPS的问题
    微信小程序校验文件在浏览器无法打开
    [Linux]使用宝塔面板做负载均衡时遇到的问题和解决办法
    [Linux]Service mysql start出错(mysql: unrecognized service)解决方法
    Linux的php-fpm优化心得-php-fpm进程占用内存大和不释放内存问题(转)
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835052.html
Copyright © 2011-2022 走看看