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;
    }


  • 相关阅读:
    tomcat日志切割脚本
    开源项目推荐:GSL科学计算函数库(GNU Scientific Library),实现VS2019源码编译
    MCUXpresso环境开发(本文争取做到您能够快速简易应用入门,知道大体流程,复杂功能和窗口设置一律不管)
    Arduino uno r3 使用 ESP8266 UARTWiFi 透传模块
    ESP8266引脚的说明
    windows10安装Trading View出错解决办法
    windows or linux 64位安装talib包
    python中list数组指定类型
    面试官问我JVM调优,我忍不住了!
    稳了!我准备了1个晚上的CMS垃圾收集器
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835052.html
Copyright © 2011-2022 走看看