• HDU 2846 Repository(字典树,每个子串建树,*s的使用)


    Repository

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 6444    Accepted Submission(s): 2096


    Problem Description
    When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
     
    Input
    There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
     
    Output
    For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
     
    Sample Input
    20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s
     
    Sample Output
    0 20 11 11 2
     
    Source
     
    Recommend
    gaojie   |   We have carefully selected several similar problems for you:  2852 2847 2845 2850 2851 
     
    根据题意可知:题目属于判断字符串中是否包含子串的问题,对于一般的字典树,用来判断前缀,而这里不能直接这么去建树。在建树的时候将字符串X=X1X2....Xn的分别以X1,X2....Xn开头的后缀子串插入到Trie树中,如此一来就可以判断某个字符串是否被包含在另一个字符串当中。但是这就有一个问题,比如插入了字符串abab,那么当查找字符串ab时就会重复计数,因此需要多设计一个标识以表示在插入"abab"和"ab"时时同一个字符串即可(是同一个字符串就不需要使计数器加1),因此在Trie树结点中多设计一个商品id来标记。id用来记录最后一个经过此路径上的商品编号,如果要插入的字符串编号同当前节点的编号不同,则计数器加1,并且将当前结点的编号置为要插入的字符串的编号。
     
     
    经验:对字符串的处理还可以这样insert(root, s + j, i);,void insert(trie*root, char *s, int id),*s就是某个s[i],s++就相当于i++
     1 #include <iostream>
     2 #include<cstring> 
     3 #include <cstdio>
     4 #include<string>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 typedef struct nn
     9 {
    10     int count;
    11     int id;
    12     nn* nxt[26];
    13 }trie;
    14 
    15 void insert(trie*root, char *s, int id)
    16 {
    17     trie*p = root;
    18     while (*s != '')//对字符串的处理还可以这样
    19     {
    20         if (p->nxt[*s - 'a'] == NULL)
    21         {
    22             trie *temp = (trie *)malloc(sizeof(trie));
    23             for (int i = 0; i<26; i++)
    24             {
    25                 temp->nxt[i] = NULL;
    26             }
    27             temp->count = 0;
    28             temp->id = -1;            //-1表示没有商品 
    29             p->nxt[*s - 'a'] = temp;
    30         }
    31         p = p->nxt[*s - 'a'];
    32         if (p->id != id)
    33         {//如果当前结点的ID不等于要插入的ID,则计数器count++,并且重新置ID的值 
    34             p->id = id;
    35             p->count++;
    36         }
    37         s++;//每一个子串,不如asda里的sd
    38     }
    39 }
    40 
    41 int search(trie*root, char *s)
    42 {
    43     trie *p = root;
    44     int i;
    45     for (i = 0; s[i] != ''; i++)
    46     {
    47         if (p->nxt[s[i] - 'a'] == NULL)
    48             return 0;
    49         p = p->nxt[s[i] - 'a'];
    50     }
    51     return p->count;
    52 }
    53 
    54 int main()
    55 {
    56     int i, j;
    57     int n, m;
    58     char s[21];
    59     trie *root = (trie*)malloc(sizeof(trie));
    60     for (i = 0; i < 26; i++)
    61     {
    62         root->nxt[i] = NULL;
    63     }
    64     root->count = 0;
    65     root->id = -1;
    66     cin >> n;
    67     for (i = 1; i <= n; i++)
    68     {
    69         cin >> s;
    70         int l = strlen(s);
    71         for (j = 0; j < l; j++)
    72         {//将字符串X=X1X2...Xn的分别以X1,X2...Xn开头的后缀字符串插入到Trie树中
    73             insert(root, s + j, i);
    74         }
    75     }
    76     cin >> m;
    77     for (i = 1; i <= m; i++)
    78     {
    79         cin >> s;
    80         cout << search(root, s) << endl;
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    细说mysql索引
    SQL常见优化Sql查询性能的方法有哪些?
    JDK提供的几种线程池比较
    JVM 内部运行线程介绍
    线上服务CPU100%问题快速定位实战
    浅谈Java中的hashcode方法
    Spring 核心组件工作原理简析
    SpringMVC工作原理
    open MMT.distributions = null on transaction type: WIP Lot Split
    OSFM Tables
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8452684.html
Copyright © 2020-2023  润新知