zoukankan      html  css  js  c++  java
  • hdu----(2222)Keywords Search(trie树)

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 35683    Accepted Submission(s): 11520


    Problem Description
    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
    Wiskey also wants to bring this feature to his image retrieval system.
    Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
    To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
     
    Input
    First line will contain one integer means how many cases will follow by.
    Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
    Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
    The last line is the description, and the length will be not longer than 1000000.
     
    Output
    Print how many keywords are contained in the description.
     
    Sample Input
    1 5 she he say shr her yasherhs
     
    Sample Output
    3
     
    Author
    Wiskey
     
    代码:
     
    字典树
     1 /*hdu 2222 字典树*/
     2 //#define LOCAL
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<iostream>
     6 using namespace std;
     7 struct Trie
     8 {
     9    struct Trie *next[26];
    10    int tail;
    11 };
    12 char str[55];
    13 char ss[1000005];
    14 void in_Trie(char *s,Trie *root)
    15 {
    16    Trie *cur=root,*newcur;
    17   for(int i=0;s[i]!='';i++)
    18   {
    19        if(cur->next[s[i]-'a']==NULL)
    20      {
    21          newcur=new Trie;   //(Trie*)malloc(sizeof(sizeof(Trie)));
    22          for(int j=0;j<26;j++)
    23            newcur->next[j]=NULL;
    24             newcur->tail=0;
    25         cur->next[s[i]-'a']=newcur;
    26      }
    27     cur=cur->next[s[i]-'a'];
    28   }
    29    cur->tail++;
    30 }
    31 int query(char *s,Trie *root)
    32 {
    33     int i=0,cnt=0;
    34     Trie *cur;
    35     for(int j=0;s[j]!='';j++)
    36     {
    37         cur=root;
    38       for(i=j;s[i]!='';i++){
    39         if(cur->next[s[i]-'a']!=NULL){
    40          cur=cur->next[s[i]-'a'];
    41             cnt+=cur->tail;
    42             cur->tail=0;  //只需求出第一次出现的
    43         }
    44        else  break;
    45       }
    46     }
    47    return cnt;
    48 }
    49 void dele(Trie *root)
    50 {
    51   for(int i=0 ; i<26 ; i++ )
    52       if(root->next[i]!=NULL)
    53       dele(root->next[i]);
    54  // free(root);
    55  delete root;
    56 }
    57 int main()
    58 {
    59   #ifdef LOCAL
    60   freopen("test.in","r",stdin);
    61   #endif
    62   int t,i,n;
    63   Trie *root;
    64   scanf("%d",&t);
    65   while(t--)
    66   {
    67        root = new Trie ;
    68        for(int j=0;j<26;j++)
    69        root->next[j]=NULL;
    70        root->tail=0;
    71      scanf("%d",&n);
    72     for(i=0;i<n;i++){
    73       scanf("%s",str);
    74       in_Trie(str,root);
    75     }
    76     scanf("%s",ss);
    77     printf("%d
    ",query(ss,root));
    78     dele(root);
    79     }
    80    return 0;
    81 }
    View Code
  • 相关阅读:
    手把手带你玩转 DialogFragment
    紧张的去京东面试7,没想到可以成功拿下offer
    这个有点强,MySQL常用优化指南及大表优化思路(值得收藏)
    Java程序员两年经验斩获头条 Offer,技术杠杠的
    为什么大家都说 SELECT * 效率低
    Java程序员想要靠外包刷题,结果却大跌眼镜,心态都崩了
    一次请求在SpringMVC核心执行流程
    工作三年经验,一年内我靠这份javaBAT进阶面试题从13K到大厂25K
    用了这么久的数据库连接池,你知道原理吗?
    poj 3295 Tautology(栈)
  • 原文地址:https://www.cnblogs.com/gongxijun/p/4001071.html
Copyright © 2011-2022 走看看