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代码:
 
 #include<stdio.h>
#include<iostream>
#include<string.h> using namespace std; struct node
{
int count;
int value;
node *next[];
node()
{
for(int i=;i<;i++)
next[i]=;
count=;
value=;
}
}; void insert(node *&root,char *s,int num)
{
int i=,t=;
node *p=root;
if(!p)
{
p=new node();
root=p;
}
while(s[i])
{
t=s[i]-'a';
if(!p->next[t])
p->next[t]=new node();
p=p->next[t];
if(p->value!=num)
{
p->count++;
p->value=num;
}
// printf("%d\n",p->value);
i++;
}
} int find(node *root,char *s)
{
int i=,t=,ans;
node *p=root;
if(!p)
return ;
while(s[i])
{
t=s[i]-'a';
if(!p->next[t])
return ;
p=p->next[t];
ans=p->count;
i++;
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
char str[];
node *root=NULL;
while(T--)
{
scanf("%s",str);
for(int i=;i<strlen(str);i++)
insert(root,str+i,T+);
}
int Q;
scanf("%d",&Q);
while(Q--)
{
scanf("%s",str);
printf("%d\n",find(root,str));
}
return ;
}
 

hdu 2846 Repository的更多相关文章

  1. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

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

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  5. HDU 2846 Repository(字典树)

    字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...

  6. HDU 2846:Repository(Trie)

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 题意:给出N个模式串,再给出M个文本串,问每一个文本串在多少个模式串中出现. 思路:平时都是找前缀的,这里 ...

  7. Repository HDU - 2846 字典树

    题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...

  8. Repository HDU - 2846 (trie)

    题中没给范围 所以控制不好数组范围..不是超内存就是runtime.. 好吧 到了晚上终于调出来数组模拟的了 题意: 求含有某字符段的个数 解析: 把每个字符串遍历一遍 以每个元素为起点建树就好了.. ...

  9. HDU 2846 (AC自动机+多文本匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 题目大意:有多个文本,多个模式串.问每个模式串中,有多少个文本?(匹配可重复) 解题思路: 传统 ...

随机推荐

  1. 【转】设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成 ...

  2. gcc的-D和-U参数:宏的设置与取消

    http://blog.chinaunix.net/uid-7213338-id-2658068.html  gcc的-D和-U参数:宏的设置与取消 2006-10-08 22:59:06 分类: L ...

  3. 初入C的世界

    大家好,我叫吉贯之,来自贵州省遵义市,现就读于北京工业大学耿丹学院信息技术系计算机与科学专业,我的学号是160809127,我喜欢运动和一些电脑方面的软件操作. 应老师要求在博客园建立的博客,地址是h ...

  4. 日志案例分析(PV,UV),以及动态分区

    1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...

  5. c语言main函数返回值、参数详解(返回值是必须的,0表示正常退出)

    C语言Main函数返回值 main函数的返回值,用于说明程序的退出状态.如果返回0,则代表程序正常退出:返回其它数字的含义则由系统决定.通常,返回非零代表程序异常退出. 很多人甚至市面上的一些书籍,都 ...

  6. ADO.NET连接到数据库(oracle)

    本文摘抄于http://www.cnblogs.com/luluping/archive/2009/10/13/1582737.html,如有侵权,请联系博主. OracleConnection 对象 ...

  7. Magento Error – The directory is not writable by server.

    When trying to use the insert image functionality in Magento if you receive an error saying: “The di ...

  8. Magento white screen or how XML can break your site?

    Magento white screen or how XML can break your site? by SANDO on 02. OCT, 2012 in MAGENTO, SMALL TIP ...

  9. Magento Table Rate运费国家代码汇总

    Magento Table Rate是三种内置未调用第三方API运费方式中最强大的一个.通过设置国家,区域,邮编,价格来划分不同的运费等级.该方式基本能够满足轻量级的B2C商城的运费模式.这里收集下国 ...

  10. thinkphp 加载静态框架frameset frame 浏览器显示空白

    我觉得静态框架这个东西非常奇怪,可能是因为没有研究透它. 我的情况是这样的,我之前做过的一个面向对象没有基于thinkPHP,的项目中用同一套后台静态框架没有问题,但用thinkphp后台的index ...