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. Error 18000 during outlook 2011 rebuild

    Thanks Rob 88 Via:https://discussions.apple.com/message/23726703#23726703 1. Make a duplicate copy o ...

  2. Calling / Running a report in Oracle forms 10g / 11g

    Calling / Running a report in Oracle forms 10g / 11g Below is the procedure to call a report in Orac ...

  3. JUnit 单元测试 配置

    选中工程,右键  built path , add liberaries , JUnit , JUnit4 这样就不用每次测试时都在main方法中写了

  4. How to crack interviews ...

    Code practice: Leetcode: www.leetcode.com HackerRank: www.hackerrank.com Topcoder: https://www.topco ...

  5. LightOJ::1077 -----奇妙的最大公约数

    题目:http://www.lightoj.com/volume_showproblem.php?problem=1077 题意:在平面上, 给出两个点的坐标 例如:(x, y) 其中x, y 都是整 ...

  6. 你未必知道的css小知识

    1:当按百分比设定一个元素的宽度时,它是相对于父容器的宽度计算的,但是,对于一些表示竖向距离的属性,例如padding-top,padding-bottom,margin-top,margin-bot ...

  7. ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'

    1.问题 在刚刚安装MySQL之后,进入到mysql环境下,创建数据库,出现下面的提示信息: ERROR 1044 (42000): Access denied for user ''@'localh ...

  8. [转载] 文件系统vs对象存储——选型和趋势

    原文: http://www.testlab.com.cn/Index/article/id/1082.html#rd?sukey=fc78a68049a14bb2699b479d5e730f6f45 ...

  9. JQuery $()后面的括号里的内容什么时候加引号,什么时候不加

    一.如果是已经声明存在的变量或者对象,就不用加引号. 比如var name=document.getElementById("name"); $(name)或者$(this). 二 ...

  10. [js] 跨域

    原文链接:http://www.cnblogs.com/scottckt/archive/2011/11/12/2246531.html 什么是跨域? 首先什么是跨域,简单地理解就是因为JavaScr ...