zoukankan      html  css  js  c++  java
  • POJ_3630 Phone List 字典树

    比较惊险的一次字典树

    继上次动态分配内存字典树,这次尝试使用静态数组存放字典树:每个trie[i][j]保存其子树的节点序号,使用全局变量更新,权值以及其余值则另外存储

    题意:判断给出的几个数字串中是否有一个是另一个的前缀

    使用字典树比较时需要处理 123 123, 123 123456, 123 12,1230 1234等情况,细节较多,不过还是可以通过添加辅助数组以及标志变量处理的。

    注:process描述可能有点差错,只供参考(反正我当时是看着写的)

    代码:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <set>
     6 #include <queue>
     7 using namespace std;
     8 #define maxnode 12
     9 #define maxn 10005
    10 
    11 /*
    12 process:
    13     string s;
    14     flag
    15     along the tree
    16     s[i] already exist?
    17         i == s_size? return 0;
    18         else go to it's sub tree
    19     No s[i] ?
    20         i != 0?
    21             flag & no hasChild? return 0;
    22             create node, refresh flag
    23         i == 0?
    24             create node, refresh flag
    25 */
    26 int trie[maxn * 10][maxnode];//0 as root
    27 bool hasChild[maxn * 10];
    28 int totindex = 1;
    29 char st[maxn][15];
    30 
    31 void init(){
    32     memset(trie, 0, sizeof(trie));
    33     memset(st, 0, sizeof(st));
    34     memset(hasChild, 0, sizeof(hasChild));
    35     totindex = 1;
    36 }
    37 
    38 int insert_find(char tms[]){
    39     bool flag = true;
    40     int len = strlen(tms), i = 0, j = 0;
    41     for(; i < len; i++){
    42         if(trie[j][tms[i] - '0']){
    43             if(i == len - 1)
    44                 return 0;
    45             j = trie[j][tms[i] - '0'];
    46         }
    47         else{
    48             if(i != 0){
    49                 if(flag && !hasChild[j]){
    50                     return 0;
    51                 }
    52                 else{
    53                     hasChild[j] = true;
    54                     flag = false;
    55                     trie[j][tms[i] - '0'] = totindex++;
    56                     j = trie[j][tms[i] - '0'];
    57                 }
    58             }
    59             else{
    60                 hasChild[j] = true;
    61                 flag = false;
    62                 trie[j][tms[i] - '0'] = totindex++;
    63                 j = trie[j][tms[i] - '0'];
    64             }
    65         }
    66     }
    67     return 1;
    68 }
    69 
    70 int main(){
    71     int t;
    72     scanf("%d", &t);
    73     while(t--){
    74         int n;
    75         init();
    76         memset(st, 0, sizeof st);
    77         scanf("%d", &n);
    78         for(int i = 0; i < n; i++){
    79             scanf("%s", st[i]);
    80         }
    81 
    82         bool flag = true;
    83         for(int i = 0; i < n; i++){
    84             int tmp = insert_find(st[i]);
    85             if(!tmp){
    86                 flag = false;
    87                 break;
    88             }
    89         }
    90 
    91         if(flag){
    92             printf("YES
    ");
    93         }
    94         else{
    95             printf("NO
    ");
    96         }
    97     }
    98 }

    题目:

    Phone List
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29377   Accepted: 8764

    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:

    • Emergency 911
    • Alice 97 625 999
    • 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
  • 相关阅读:
    C# 字符串多行显示、文本换行
    安卓TextView显示图片与文字作为底部菜单
    在Eclipse中载入安卓SDK框架源代码(部分)
    Android分享到腾讯微博,信息,新浪微博等等,的实现方式
    PHP运行环境配置
    Android布局设计之ExpandableList绑定XML数据构成级联、item布局页面的控件查找及配置child事件,自定义适配显示内容
    Flash开发Android应用
    Android布局设计之ListView使用XML数据源来分页加载
    移动应用开发视频教学下载大全(VeryCD上)
    前端 关于请求地址时出现乱码, 出现%E2%80%8B的问题
  • 原文地址:https://www.cnblogs.com/bolderic/p/6798352.html
Copyright © 2011-2022 走看看