zoukankan      html  css  js  c++  java
  • hdu 2846 Repository

    http://acm.hdu.edu.cn/showproblem.php?pid=2846

    Repository

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


    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
     
    分析:
     
    典型的字典树问题。
     
    AC代码:
     
     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<string.h>
     4 
     5 using namespace std;
     6 
     7 struct node
     8 {
     9     int count;
    10     int value;
    11     node *next[26];
    12     node()
    13     {
    14         for(int i=0;i<26;i++)
    15             next[i]=0;
    16         count=0;
    17         value=0;
    18     }
    19 };
    20 
    21 void insert(node *&root,char *s,int num)
    22 {
    23     int i=0,t=0;
    24     node *p=root;
    25     if(!p)
    26     {
    27         p=new node();
    28         root=p;
    29     }
    30     while(s[i])
    31     {
    32         t=s[i]-'a';
    33         if(!p->next[t])
    34             p->next[t]=new node();
    35         p=p->next[t];
    36         if(p->value!=num)
    37         {
    38             p->count++;
    39             p->value=num;
    40         }
    41     //    printf("%d
    ",p->value);
    42         i++;
    43     }
    44 }
    45 
    46 int find(node *root,char *s)
    47 {
    48     int i=0,t=0,ans;
    49     node *p=root;
    50     if(!p)
    51         return 0;
    52     while(s[i])
    53     {
    54         t=s[i]-'a';
    55         if(!p->next[t])
    56             return 0;
    57         p=p->next[t];
    58         ans=p->count;
    59         i++;
    60     }
    61     return ans;
    62 }
    63 
    64 int main()
    65 {
    66     int T;
    67     scanf("%d",&T);
    68     char str[21];
    69     node *root=NULL;
    70     while(T--)
    71     {
    72         scanf("%s",str);
    73         for(int i=0;i<strlen(str);i++)
    74             insert(root,str+i,T+1);
    75     }
    76     int Q;
    77     scanf("%d",&Q);
    78     while(Q--)
    79     {
    80         scanf("%s",str);
    81         printf("%d
    ",find(root,str));
    82     }
    83     return 0;
    84 }
    View Code
     
  • 相关阅读:
    Js面向对象
    JavaScript:this是什么?
    vs2010 连接 oracle 11g
    Oracle 数据库 数据文件 表 表空间 用户的关系
    消息推送
    Zookeeper's Bug
    hadoop启动之后没有namenode和secondarynamenode
    大数据zookeeper connect time out
    Silverlight中需要用到模板选择器(DataTemplateSelector)的替代方案
    当SQL Server数据库遇到Oracle数据交互接口
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4449599.html
Copyright © 2011-2022 走看看