Phone List(字典樹,數據結構)

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

解題思路

用的靜態數組寫的,出現NO的情況有兩,一是輸入的字符串先短後長,長字符串會經過,之前字符串的終止節點; 

二是輸入的字符串先長後短,短字符串不會產生新的節點。



AC代碼(1)

#include <cstdio>
#include <cstring>

int ch[100000][27], finish[100000][27];                           //ch[][]記錄節點編號,第幾個節點經某個元素到第幾個節點,finish[][]記錄結束節點
int num_jd;                                                       //num_id 節點的個數
int val[100000];                                                  //val[]記錄了每個節點的分支數,本題中沒用
int flag1, flag2;

void init()
{
    num_jd = 1;
    memset(ch, 0, sizeof(ch));
    memset(val, 0, sizeof(val));
    memset(finish, 0, sizeof(finish));
}

void insert(char *s)
{
    int u = 0, i, c, len = strlen(s);
    for(i = 0; i < len; i++)
    {
        c = s[i] - '0';
        if(!ch[u][c])
        {
            ch[u][c] = num_jd++;
            flag1 = 1;
        }
        if(finish[u][c])
            flag2 = 1;
        if(i == len - 1)
            finish[u][c] = 1;
        u = ch[u][c];
        val[u]++;
    }

}

int main()
{
    int T, h, N;
    char str[15];
    scanf("%d", &T);
    while(T--)
    {
        init();
        scanf("%d", &N);
        flag1 = 0, flag2 =0;
        for(h = 0; h < N; h++)
        {
            scanf("%s", str);
            if(h > 0 && !flag1 || flag2)
                continue;
            flag1 = 0, flag2 =0;
            insert(str);
        }
        if(!flag1 || flag2)
        {
            printf("NO\n");
            continue;
        }
        else
            printf("YES\n");
    }
    return 0;
}




AC代碼(2)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char phone[10000][15];
int cmp(const void* a,const void* b)
{
    return strcmp((char*)a, (char*)b);
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        int i, n;
        bool flag = false;
        scanf("%d", &n);
        for(i = 0; i < n; i++)
        {
            scanf("%s", phone[i]);
        }
        qsort(phone, n, sizeof(phone[0]), cmp);
        for(i = 0; i < n - 1; i++)
        {
            if(strncmp(phone[i], phone[i+1], strlen(phone[i])) == 0)
            {
                flag = true;
                break;
            }
        }
        if(flag == false)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章