Repository

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

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
 
题意: 给出一些字符,然后寻问一个字符,在给出字符里能找到子串的个数..
 
代码:

 //#define LOCAL
#include<cstdio>
#include<cstring>
typedef struct node
{
struct node *child[];
int cnt; //作为统计
int id;
}Trie; void Insert(char *s,Trie *root,int id)
{
int pos,i;
Trie *cur=root,*curnew;
for(;*s!='\0';s++)
{
pos=*s-'a';
if(cur->child[pos]==NULL)
{
curnew = new Trie;
for( i=; i<;i++)
curnew->child[i]=NULL;
curnew->cnt=;
curnew->id=;
cur->child[pos]=curnew;
}
cur=cur->child[pos];
if(cur->id!=id) //避免同一个单词重复计算子串
{
cur->cnt++;
cur->id=id;
}
}
} int query(char *s, Trie *root)
{
int pos;
Trie *cur=root;
while(*s!='\0')
{
pos=*s-'a';
if(cur->child[pos]==NULL)
return ;
cur=cur->child[pos];
s++;
}
return cur->cnt;
}
void del(Trie *root)
{
Trie *cur=root;
for(int i=;i<;i++)
{
if(cur->child[i]!=NULL)
del(cur->child[i]);
}
delete cur;
return ;
}
char str[];
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int n,m,i;
scanf("%d",&n);
Trie *root=new Trie;
for( i=;i<;i++)
root->child[i]=NULL;
root->cnt=;
while(n--)
{
scanf("%s",str);
for(i= ; str[i]!='\0' ;i++)
Insert(str+i,root,n+);
}
scanf("%d",&m);
while(m--)
{
scanf("%s",str);
printf("%d\n",query(str,root));
}
del(root);
return ;
}

hdu----(2848)Repository(trie树变形)的更多相关文章

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

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

  2. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

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

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

  4. hdu 2846 Repository (字典树)

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

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

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

  6. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  7. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  8. hdu 5269 trie树

    现场想到了lowbit(X xor Y)=X和Y从右向左数,对应相同的数的个数+1...然而并没有想到接下来用trie树 然后就想排个序试试...然后就整个人都不好了啊摔 sol:用trie,一边in ...

  9. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

随机推荐

  1. [转][Unreal3教程]引擎使用操作入门教程

    前言 如果你想转载这篇文章,请不要把这篇文章的来源去掉:http://blog.sina.com.cn/zjmjeo 一来是对我辛苦写这篇文章肯定,二来我可以有更多机会和你们这些朋友交流 1.打开引擎 ...

  2. [HDOJ5543]Pick The Sticks(DP,01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5543 题意:往长为L的线段上覆盖线段,要求:要么这些线段都在L的线段上,要么有不超过自身长度一半的部分 ...

  3. Messages消息处理

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. XML详解:第一部分

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. SQL GUID和自增列做主键的优缺点

    我们公司的数据库全部是使用GUID做主键的,很多人习惯使用int做主键.所以呢,这里总结一下,将两种数据类型做主键进行一个比较. 使用INT做主键的优点: 1.需要很小的数据存储空间,仅仅需要4 by ...

  6. hdu 0-1背包

    题目地址http://acm.hdu.edu.cn/showproblem.php?pid=2602 #include <stdio.h> #include <string.h> ...

  7. python tools: iPython Notebook

    Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...

  8. JS学习笔记(五) HTML DOM

    参考资料: 1. http://www.w3school.com.cn/js/js_htmldom.asp 2. http://www.runoob.com/htmldom/htmldom-tutor ...

  9. [poj2785]4 Values whose Sum is 0(hash或二分)

    4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 19322 Accepted: ...

  10. Spring security3

    最近一直在学习spring security3,试着搭建了环境: 构建maven环境 项目配置pom.xml文件 <project xmlns="http://maven.apache ...