麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Phone List

2019-11-11 04:47:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Phone List

時(shí)間限制:1000 ms  |  內(nèi)存限制: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

解題報(bào)告:用字典樹

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;  //標(biāo)記是否是一個(gè)單詞}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) //創(chuàng)建一棵字典樹,與查找合并{    int len = strlen(str);    Trie *p = root, *q;    for(int i=0; i<len; i++)    {        if(p->flag==1) //查找1;說(shuō)明已有一個(gè)單詞作為前綴,比如119,119895            return 1;        int id = str[i]-'0'; //數(shù)字字符        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; //一個(gè)單詞    return 0;}void dealTrie(Trie* T) //清理內(nèi)存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; //默認(rèn)YES        char s[15];        for(int i=0;i<n;i++){            getchar();            scanf("%s",s);            if(!flag) //把數(shù)據(jù)讀完                continue;            if(createTrie(s)){                flag=0;            }        }        if(flag)            printf("YES/n");        else            printf("NO/n");        dealTrie(root); //釋放內(nèi)存,否則超內(nèi)存    }    return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品久久久久久久久久三级 | 免费一区区三区四区 | 午夜视 | 成人午夜在线免费视频 | 国产做爰全免费的视频黑人 | 国产91小视频在线观看 | 成人在线观看网 | 精品国内视频 | 亚洲成人中文字幕在线 | 黄色影院一级片 | 狠狠干五月天 | 久久久精品99 | 13一14毛片免费看 | 9797色| 久久精品国产久精国产 | 天天好比网| 午夜影视一区二区 | 国产精品99久久久久久久女警 | 欧美成人高清视频 | 99精品视频久久精品视频 | 妇子乱av一区二区三区 | 蜜桃久久一区二区三区 | 国产亚洲精彩视频 | 国产成人高清在线 | 草草视频在线播放 | 亚洲网站在线观看视频 | 免费1级做55爰片l在线观看 | av影院在线播放 | 一级做a爱视频 | 香蕉国产片 | 国产一级做a爰片在线看 | 精品一区二区三区在线观看视频 | av在线免费观看中文字幕 | 精品亚洲一 | 国产精品久久久久永久免费 | 亚洲国产精品久久久久久久久久 | 国产在线1区 | 韩毛片| 视频一区二区中文字幕 | 国产一级毛片国产 | 成年免费观看视频 |