zoukankan      html  css  js  c++  java
  • Hdoj 1671

    原题链接

    描述

    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.

    输入

    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.

    输出

    For each test case, output “YES” if the list is consistent, or “NO” otherwise.

    样例输入

    2
    3
    911
    97625999
    91125426
    5
    113
    12340
    123440
    12345
    98346

    样例输出

    NO
    YES

    思路

    字典树
    令人悲伤,数组形式的字典树MLE了,把数组大小改小又WA,原以为只能用链表了,群里大佬说能用vector形式!
    我以前没用过vector,赶紧百度学了一下,上手套模版,稍微改动一下就好。期间犯了很多很傻的错误,比如忘记给vector迭代下去之类的,觉得自己好渣啊~

    代码

    #include <bits/stdc++.h>
    #define maxn 900000
    using namespace std;
    
    struct node
    {
    	int a[10];
    };
    
    vector<node> trie;
    int cnt[maxn], tot, f;
    
    void create(char st[])
    {
    	int len = strlen(st);
    	int u = 0;
    	for(int i = 0; i < len; i++)
    	{
    		if(trie[u].a[st[i] - '0'] == 0)
    		{
    			trie[u].a[st[i] - '0'] = ++tot;
    			node pnew; 
    			for(int j = 0; j <10; j++) pnew.a[j] = 0;
    			trie.push_back(pnew);
    		}
    		u = trie[u].a[st[i] - '0'];
    		if(cnt[u]) f = 1;
    	}
    	cnt[u] = 1;
    	for(int i = 0; i < 10; i++) 
    		if(trie[u].a[i]) f = 1;
    }
    
    int main()
    {
    	int num; scanf("%d", &num);
    	while(num--)
    	{
    		trie.clear();
    		memset(cnt, 0, sizeof(cnt));
    		f = 0; tot = 0;
    		node pnew; for(int j = 0; j <10; j++) pnew.a[j] = 0;
    		trie.push_back(pnew);
    		int n;
    		scanf("%d", &n);
    		for(int i = 0; i < n; i++)
    		{
    			char st[12]; scanf("%s", st);
    			if(f == 0) create(st);
    		}
    		if(f == 0) printf("YES
    ");
    		else printf("NO
    ");
    	}
    	return 0;
    }
    
  • 相关阅读:
    深入理解Java:注解(Annotation)自定义注解入门
    Java基础之理解Annotation
    junit常用注解详细说明
    能判断是电脑端还是手机端的javascript
    Ext.js多文件选择上传,
    StringBuffer类和String类(原文地址 : http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.html)
    FileItem类的常用方法(关于文件上传的)
    js保留小数点后面几位的方法
    如何将div中的内容设置为空同时还要保留div本身
    使用html中的<input>标签上传多个文件(转)
  • 原文地址:https://www.cnblogs.com/HackHarry/p/8379377.html
Copyright © 2011-2022 走看看