HDU 1671 Phone List

  字典树,注意释放内存,否则MLE

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char a[10010][20];
struct node
{
    int num;
    node *childs[10];
    node()
    {
        num = 0;
        for(int i = 0; i < 10; i++)
            childs[i] = NULL;
    }
};
node *root = new node;
node *nownode,*newnode;
void Insert(char *str)
{
    nownode = root;
    int lens = strlen(str);
    for(int i = 0; i < lens; i++)
    {
        int m = str[i] - ‘0‘;
        if(nownode->childs[m] != NULL)
        {
            nownode = nownode->childs[m];
            nownode->num++;
        }
        else
        {
            newnode = new node;
            ++newnode->num;
            nownode->childs[m] = newnode;
            nownode = nownode->childs[m];
        }
    }
}
int Find(char *str)
{
    nownode = root;
    int lens = strlen(str);
    for(int i = 0; i < lens; i++)
    {
        int m = str[i] - ‘0‘;
        nownode = nownode->childs[m];
    }
    return nownode->num;
}
void del(node *root)
{
    for(int i=0; i<10; i++)
    {
        if(root->childs[i]!=NULL)
        {
            del(root->childs[i]);
        }
    }
    delete(root);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        root = new node;
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%s",a[i]);
            Insert(a[i]);
        }
        bool flag = true;
        for(int i = 0; i < n; i++)
        {
            if(Find(a[i]) != 1)
            {
                flag = false;
                break;
            }
        }
        if(flag)puts("YES");
        else puts("NO");
        del(root);
    }
    return 0;
}
时间: 2024-10-16 14:14:14

HDU 1671 Phone List的相关文章

HDU 1671 Phone List(Trie的应用与内存释放)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18217    Accepted Submission(s): 6120 Problem Description Given a list of phone numbers, determine if it is consistent in the sense th

字典树 Trie (HDU 1671)

Problem 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: 1. Emergency 911 2. Alice 97 625 999 3. Bob 91 12 54 26 In this

HDU 1671 Phone List(字符处理)

题目 用字典树可以过,可是我写的字典树一直各种错误,,, 所以,我用了别的更简便的方法.. //去你妹的一直有问题的字典树!!! ////字典树,树的根是空的 // ////#include<iostream> //#include<cstdio> ////#include<list> //#include<algorithm> //#include<cstring> ////#include<string> ////#include

HDU 1671 Phone List (Trie树 好题)

Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11721    Accepted Submission(s): 3982 Problem Description Given a list of phone numbers, determine if it is consistent in the sense th

(字典树)HDU - 1671 Phone List

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 题意:给很多电话号码,如果在拨号的时候,拨到一个存在的号码,就会直接打出去,以致以这个号码为前缀的所有其他比这个号码长的号码将不能拨出,问是不是所有的号码都能拨. 分析:可以直接建立字典树,节点中用boolean变量表示当前是否组成电话号码,一旦在遍历完某条号码之前,已经出现存在号码,则发现问题,返回false,否则true. 我使用了一个反过来的方法,即只统计前缀出现次数,遍历完某条号码,如

poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&amp;静态创建】

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25160   Accepted: 7641 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 catalogu

HDU 1671 (字典树统计是否有前缀)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem 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: 1. Emergenc

字典树模板+HDU 1671 ( Phone List )(字典树)

字典树指针模板(数组模板暂时还没写): 1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=26; 6 const int maxn=1e4+100; 7 int N; 8 9 struct Trie 10 { 11 Trie *next[MAX]; 12 int v;///v要灵活使用,具体情况具体分析 13 }; 14

hdu 1671&amp;&amp;poj 3630 Phone List 【字典树】

题目链接:http://acm.acmcoder.com/showproblem.php?pid=1671 题意:问是否存在一个串是另一个串的前缀. 解法:建字典树,插入的串的结尾设置标志位,如果以后访问到,则存在一个串是另一个串的前缀.注意释放内存,不然超内存:(太弱,释放内存调了好久... 代码: #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm>

hdu 1671 Phone List(给定n个电话号码串,问这n个电话号码串中是否存在某一串是其它串的前缀,如果存在输出NO,否则YES)

1.动态申请的内存用完以后要释放,否则超内存. 2.代码: #include<cstdio> #include<cstring> #include<stdlib.h> using namespace std; struct Node { int cnt; Node *next[10]; void init() { cnt=0; for(int i=0;i<10;i++) { next[i]=NULL; } } }; Node *p_root; int n; voi