国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Phone List

2019-11-11 06:47:48
字體:
來源:轉載
供稿:網友

Phone List

時間限制:1000 ms  |  內存限制:65535 KB難度:4描述

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 911Alice 97 625 999Bob 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 ≤ 10, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 100000. 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.樣例輸入
2391197625999911254265113123401234401234598346樣例輸出
NOYES

解題報告:用字典樹

code:

#include<iostream>#include<stdio.h>#include<queue>#include<vector>#include<stack>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int maxn=100005;const int MAX=10;typedef struct node{    struct node *next[MAX];    int flag;  //標記是否是一個單詞}Trie;Trie *root;/*root要初始化root=(Trie *)malloc(sizeof(Trie));root->flag=0;for(int i=0;i<MAX;i++){    root->next[i]=NULL;}*/int createTrie(char *str) //創建一棵字典樹,與查找合并{    int len = strlen(str);    Trie *p = root, *q;    for(int i=0; i<len; i++)    {        if(p->flag==1) //查找1;說明已有一個單詞作為前綴,比如119,119895            return 1;        int id = str[i]-'0'; //數字字符        if(p->next[id] == NULL)        {            q = (Trie *)malloc(sizeof(Trie));            q->flag = 0;    //初始v==1            for(int j=0; j<MAX; j++)                q->next[j] = NULL;            p->next[id] = q;        }        p = p->next[id];    }    for(int i=0;i<MAX;i++){ //查找2;判斷該單詞是否是其它單詞的前綴,如119895,119        if(p->next[i]!=NULL)            return 1;    }    p->flag=1; //一個單詞    return 0;}void dealTrie(Trie* T) //清理內存root{    for(int i=0;i<MAX;i++)    {        if(T->next[i]!=NULL)            dealTrie(T->next[i]);    }    free(T);}int main(){  //  freopen("input.txt","r",stdin);    int t,n;    scanf("%d",&t);    while(t--){        root=(Trie *)malloc(sizeof(Trie)); //初始化        root->flag=0;        for(int i=0;i<MAX;i++){            root->next[i]=NULL;        }        scanf("%d",&n);        int flag=1; //默認YES        char s[15];        for(int i=0;i<n;i++){            getchar();            scanf("%s",s);            if(!flag) //把數據讀完                continue;            if(createTrie(s)){                flag=0;            }        }        if(flag)            printf("YES/n");        else            printf("NO/n");        dealTrie(root); //釋放內存,否則超內存    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汕尾市| 巫溪县| 理塘县| 色达县| 冷水江市| 德化县| 闽侯县| 西贡区| 贡山| 济阳县| 桐柏县| 江山市| 桂林市| 长春市| 新源县| 东丰县| 棋牌| 临高县| 延长县| 潍坊市| 雷山县| 巴里| 赣州市| 永善县| 南部县| 蓝田县| 西乌| 九龙城区| 刚察县| 林周县| 韶关市| 静海县| 英吉沙县| 定结县| 河津市| 雷波县| 凤凰县| 大悟县| 新津县| 青阳县| 通道|