HDU 1671 Phone List 字典树(Trie)

来源:互联网 发布:炒股软件接口 编辑:程序博客网 时间:2024/04/30 16:01
<div id="contest_tabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all" style="position: relative; padding: 0.2em; font-family: Verdana, Arial, sans-serif; border: 1px solid rgb(170, 170, 170); color: rgb(34, 34, 34); border-radius: 4px; min-width: 0px; background: transparent;"><div id="problem" aria-labelledby="ui-id-2" class="ui-tabs-panel ui-widget-content ui-corner-bottom" role="tabpanel" aria-hidden="false" style="border: 0px solid rgb(170, 170, 170); border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; padding: 1em 1.4em; font-size: 14px; background: 0px 50%;"><div class="plm" style="text-align: center; font-family: Arial, Helvetica, sans-serif; font-size: 12pt; clear: both;"><div class="ptt" id="problem_title" style="font-size: 18pt; font-weight: bold; color: blue; padding: 10px;"><span style="color: green;">A - </span>Phone List</div><span id="crawlSuccess" class="crawlInfo" style="display: inline;"><strong>Time Limit:</strong><span id="timeLimit">1000</span>MS     <strong>Memory Limit:</strong><span id="memoryLimit">32768</span>KB     <strong>64bit IO Format:</strong><span id="_64IOFormat">%I64d & %I64u</span></span><div id="problem_opt" style="font-size: 12px; margin-top: 10px;"><a target=_blank id="submit" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Submit</span></a> <a target=_blank id="problem_status" href="http://acm.hust.edu.cn/vjudge/contest/126553#status//A/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Status</span></a> <a target=_blank id="problem_practice" target="_blank" href="http://acm.hust.edu.cn/vjudge/problem/18353" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">Practice</span></a> <a target=_blank id="problem_origin" target="_blank" href="http://acm.hust.edu.cn/vjudge/problem/18353/origin" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" style="display: inline-block; position: relative; padding: 0px; margin-right: 0.1em; cursor: pointer; vertical-align: middle; overflow: visible; text-decoration: none; font-family: Verdana, Arial, sans-serif; font-size: 1em; border: 1px solid rgb(211, 211, 211); color: rgb(85, 85, 85); border-radius: 4px; background: url(http://acm.hust.edu.cn/vjudge/jquery-ui-1.11.1.custom/images/ui-bg_glass_75_e3e4f8_1x400.png) 50% 50% repeat-x rgb(227, 228, 248);"><span class="ui-button-text" style="display: block; padding: 0.4em 1em;">HDU 1671</span></a></div></div><div style="width: 960px; margin: auto;"><div id="desc_A_0" class="desc_template"><div class="vj_description"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Description</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><div class="panel_content">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. </div></div></div><div class="vj_input"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);">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.</div></div><div class="vj_output"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Output</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);">For each test case, output “YES” if the list is consistent, or “NO” otherwise.</div></div><div class="vj_sampleInput"><p class="pst" style="font-family: Arial, Helvetica, sans-serif; font-size: 18pt; font-weight: bold; color: blue; margin-bottom: 0px;">Sample Input</p><div class="textBG" style="border-radius: 10px; padding: 10px; border: 2px dotted; font-family: 'Times New Roman', Times, serif; font-size: 17px; background-color: rgb(234, 235, 255);"><pre style="white-space: pre-wrap; word-wrap: break-word;">2391197625999911254265113123401234401234598346

Sample Output

NOYES


这一道题的主要思想就是字典树,,可以是字母,也可以是数字,总之就是要将所有的串依次传到树函数里面去检索。。如果不存在,则将此串归并到字典树中去,,否则就检索其是否存在。。
再者说字典树是一个怎样的结构呢,,他首先有一个头指针,,然后他的下面就是分为好多层,每一层都会有——所有有可能的元素。。比如有26个小写字母,,然后a-z的下面还会再有新的一层这样的结构。。
这个题的题意就是说给出的字符串中,是否有该串是其他穿的前缀,,是则输出NO,反之输出YES。。
//这是用一种二维数组的方法来进行连接的一种思想。。
#include <cstdio>#include <iostream>#include <string>#include <cmath>#include <algorithm>#include <cstring>#include <queue>#include <map>#define INF 0x3f3f3f3#define MAX_N  1000000000using namespace std;char a[20];int mark[100010];int DW[100100][10];int kk;bool flag = true;void solve(char *s){    int len = strlen(s);    int p = 0,getc = 0;    for(int i = 0; i<len; i++)    {        p = s[i] - '0';        if(mark[getc]) flag = false;        if(!DW[getc][p])        {            DW[getc][p] = kk++;        }        getc = DW[getc][p];    }    mark[getc] = 1;    for(int i = 0; i<10; i++)    {        if(DW[getc][i])            flag = false;    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(DW,0,sizeof(DW));        memset(mark,0,sizeof(mark));        kk = 1;        flag = true;        int n;        scanf("%d",&n);        for(int i = 0; i<n; i++)        {            scanf("%s",a);            solve(a);        }        if(flag) cout<<"YES"<<endl;        else cout<<"NO"<<endl;    }    return 0;}

这是一种比较正统的字典树的思想。。
#include <cstdio>#include <iostream>#include <string>#include <cmath>#include <algorithm>#include <cstring>#include <queue>#include <map>#define INF 0x3f3f3f3#define MAX_N  10using namespace std;struct trie{    bool flag;    trie * next[10];};trie * head = nullptr;bool fly = false;char s[15];void Insert(char *s){    trie *p = head;    int len = strlen(s);    for(int i = 0; i<len; i++)    {        int k = s[i] - '0';        if(p -> next[k] == nullptr)        {            trie * now = new trie;            now -> flag = false;            for(int j = 0; j<MAX_N; j++)            {                now -> next[j] = nullptr;            }            p -> next[k] = now;        }        if(p->flag)        {            fly = true;        }        p = p -> next[k];    }    p -> flag = true;    for(int i = 0; i<MAX_N; i++)    {        if(p -> next[i] != nullptr)        {            fly = true;            break;        }    }}void Delete(trie * pp){    for(int i = 0; i<MAX_N; i++)    {        if(pp -> next[i] != nullptr)        {            Delete(pp -> next[i]);        }    }     delete pp;//这一步一定要注意,,刚开始写在了for里面,,导致编译错误或WA,因为它需要把这一层中的所有下一层都找到以后再进行释放。。所以要写在外面。
}int main(){    int t;    scanf("%d",&t);    while(t--)    {        head = new trie;        head -> flag = false;        fly = false;        for(int i = 0; i<MAX_N; i++)        {            head -> next[i] = nullptr;        }        int n;        scanf("%d",&n);        for(int i = 0; i<n; i++)        {            scanf("%s",s);            Insert(s);        }        if(fly) cout<<"NO"<<endl;        else cout<<"YES"<<endl;        Delete(head);    }    return 0;}

0 0
原创粉丝点击