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. 四 mybatis开发dao的方法

    mybatis开发dao的方法 1.1     SqlSession使用范围 1.1.1     SqlSessionFactoryBuilder //以流的方式读取总的配置文件 Reader rea ...

  2. Delphi 缩放图像代码 - 支持PNG透明通道

    要求Delphi2007或者更高版本, 系统要求至少XP-SP2以上 实际上是利用了Windows的windowscodecs.dll这个文件的功能 在VCL里已经封装为TWICImage类 proc ...

  3. sqlserver快速查找所有存储过程中是否包含某字符

    --将text替换成你要查找的内容 select name from sysobjects o, syscomments s where o.id = s.id and text like '%tex ...

  4. SQL 2008 RAISERROR语法在SQL 2012/2014不兼容问题

    原文 旧的RAISERROR语法在SQL 2012不兼容问题 raiserror 写法: SQL 2008: raiserror 55030 'text error' SQL 2012: raiser ...

  5. 【C++】函数指针宏定义

    看耗子叔文章学习虚函数表(http://blog.csdn.net/haoel/article/details/1948051)的时候被例子的第一句惊到了 typedef void(*Fun)(voi ...

  6. (转载)C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind

    http://blog.sina.com.cn/s/blog_61437b3b0102v0bt.html http://blog.csdn.net/chenwk891/article/details/ ...

  7. ecshop transport.js 和 jquery 冲突解决办法

    您提供一个简单的解决transport.js 和 jquery 方法: 在 page_header.lbi 库文件中加入如下代码,注意操作顺序: 1.先导入transport.js  文件 {inse ...

  8. PresentViewController切换界面(一些系统自带的页面切换动画)

    视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...

  9. Java学习-033-JavaWeb_002 -- 网页标记语言JSP基础知识

    JSP 是 Sun 公司提倡的一门网页技术标准.在 HTML 文件中,加入 Java 代码就构成了 JSP 网页,当 Web 服务器访问 JSP 请求的时候,首先执行其中的 Java 程序源码,然后以 ...

  10. .NET Framework 4 和 .NET Framework 4 Client Profile

    提出这个问题的背景:在新项目中封装了一个DAL来通过EF框架对数据库操作,但是在项目引用后,每当编译时就会出错!更加诡异的是在Web项目和WCF项目中对此DAL引用时就不会出现此问题.作为一个初学者, ...