现在的位置: 首页 > 综合 > 正文

比赛 A – Phone List(字典序)

2014年10月15日 ⁄ 综合 ⁄ 共 2605字 ⁄ 字号 评论关闭

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 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
 
一看到这道题就感觉是字典序,于是就按模板写了,但是提交了好多次都显示内存超限,不知道什么原因,后来问了大神说是模板有问题,没有删除,所以就换了一个模板,就AC了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

struct TrieNode
{
  int cnt;
  TrieNode* next[10];
  TrieNode()
  {
    cnt=0;
    memset(next,0,sizeof(next));
  }
};

TrieNode* root=new TrieNode;

void build(char* s)
{
    TrieNode* p=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        if(!p->next[s[i]-'0'])
        p->next[s[i]-'0']=new TrieNode;
        p=p->next[s[i]-'0'];
        p->cnt++;
    }
}

int Search(char* s)
{
    TrieNode* p=root;
    int len=strlen(s);
    for(int i=0;i<len;i++)
        p=p->next[s[i]-'0'];
    return p->cnt;
}

void Del(TrieNode* p)
{
    for(int i=0;i<10;i++)
    {
        if(p->next[i]!=NULL)
            Del(p->next[i]);
    }
    free(p);
}

int main()
{
    int t,n,i;
    char str[10001][30];
    scanf("%d",&t);
    while(t--)
    {
        root=new TrieNode;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            build(str[i]);
        }
        int flag=0;
        for(i=0;i<n;i++)
        {
            if(Search(str[i])!=1)
            {
                flag=0;
                break;
            }
            flag=1;
        }
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
        Del(root);
    }
    return 0;
}

希望有大神帮我看看我的怎么改才对:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <malloc.h>
using namespace std;
char a[10005][15];
struct Trie
{
    int v;//v可以根据实际情况任意变化,在这里v是每个字母的次数;
    Trie *next[10];
} tree[100000];
Trie root;
void createTrie(char *str)//建立字典树;
{
    int len=strlen(str);
    Trie *p=&root,*q;
    for(int i=0; i<len; i++)
    {
        int id=str[i]-'0';
        if(p->next[id]==NULL)
        {
            q=(Trie *)malloc(sizeof(root));//申请一块新内存;
            q->v=0;//v遇到新字母每一层都初始化为1;
            for(int j=0; j<10; j++)
                q->next[j]=NULL;
            p->next[id]=q;
            p=p->next[id];
        }
        else
        {
            p->next[id]->v++;//当第一个输入的字符串和后面又相等的时候,v++;
            p=p->next[id];
        }
    }
}
int findTrie(char *str)//在字典树里查询;
{
    int len=strlen(str);
    Trie *p=&root;
    for(int i=0; i<len; i++)
    {
        int id=str[i]-'0';
        p=p->next[id];
        if(p==NULL)
            return 0;
    }
    return p->v;//相同的字母个数;
}
int main()
{
    int t,n,ans;
    cin>>t;

    for(int i=0; i<10; i++)
        root.next[i]=NULL;
    while(t--)
    {
        cin>>n;
        ans=1;
        for(int i=1; i<=n; i++)
        {
            cin>>a[i];
            createTrie(a[i]);
        }
        for(int i = 1; i <= n; i++)
        {
            if(findTrie(a[i]))
            {
                ans=0;
            }
        }
        if(ans==0)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }

    return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.