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. vs2012 提示 未能正确加载 "Visual C++ Language Manager Package" 包 的解决办法

    1.点击vs2012菜单栏 工具-> Visual Studio 命令提示 打开命令窗口 2.输入命令 "devenv /Setup" 3.重新打开vs2012

  2. 各种编码中汉字所占字节数;中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030

    vim settings set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936,latin1set termencoding=utf-8se ...

  3. ServiceStack.OrmLite 笔记2 -增

    ServiceStack.OrmLite 笔记2 这篇主要介绍 增加 db.Insert(new Employee { Id = 1, Name = "Employee 1" }) ...

  4. 2016.3.22考试(HNOI难度)

    T1 盾盾的打字机 盾盾有一个非常有意思的打字机,现在盾哥要用这台打字机来打出一段文章. 由于有了上次的经验,盾盾预先准备好了一段模板A存在了内存中,并以此为基础来打出文章B.盾盾每次操作可以将内存中 ...

  5. Codeforces Round #378 (Div. 2) C. Epidemic in Monstropolis 模拟

    C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. idea编辑器HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath();方法无法使用

    HttpServlet httpServlet = ServletActionContext.getServletContext().getRealPath(); 前几天在使用idea的时候发现这个方 ...

  7. 细说 Request[]与Request.Params[]

    http://www.cnblogs.com/fish-li/archive/2011/12/06/2278463.html

  8. 直播未来属于RTMP还是HTTP?

    直播未来属于RTMP还是HTTP? HTTP 传视频比 RTMP 实现起来简单?HTTP 延迟太高? 答:直播通讯未来是属于html5的. 1,协议使用份额 如今国内90%的面向大众的直播平台都是采用 ...

  9. android模拟器中文乱码

    问题:在xml文件中设置的中文能正确输出,但是在java文件中设置的中文会在模拟器上呈现乱码 解决方案:在build.gradle文件中添加一行代码 android {compileOptions.e ...

  10. C++——友元、异常和其他

    一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...