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


  • 相关阅读:
    servlet+jsp结合完成加法运算
    Servlet入门
    java之二叉树算法
    Session
    cookie
    jsp之jstl
    《HDAO》
    《Zoom An Image With Different Interpolation Types》
    《SSAO》
    《debug unreal engine code》
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835052.html
Copyright © 2020-2023  润新知