HDU 2846 Repository (字典树 后缀建树)
Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2932 Accepted Submission(s): 1116
Problem Description
Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
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.
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
0
20
11
11
2
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846
题目大意:p个字符串,q个关键词,问有多少个字符串包括关键词
题目分析:对于每一个字符串,我们依照其后缀建立字典树,建树时须要加1个id。表示这个分支来自第几个字符串,不然会反复计数。比方例子的第三组查询
#include <cstdio>
#include <cstring>
char s[25];
int id; struct node
{
node *next[26];
int cnt;
int id;
node()
{
memset(next, NULL, sizeof(next));
cnt = 0;
id = -1;
}
}; void Insert(node *p, char *s, int index, int id)
{
for(int i = index; s[i] != '\0'; i++)
{
int idx = s[i] - 'a';
if(p -> next[idx] == NULL)
p -> next[idx] = new node();
p = p -> next[idx];
if(p -> id != id)
{
p -> id = id;
p -> cnt ++;
}
}
} int Search(node *p, char *s)
{
for(int i = 0; s[i] != '\0'; i++)
{
int idx = s[i] - 'a';
if(p -> next[idx] == NULL)
return 0;
p = p -> next[idx];
}
return p -> cnt;
} int main()
{
int p, q;
id = 0;
node *root = new node();
scanf("%d", &p);
for(int i = 0; i < p; i++)
{
scanf("%s", s);
int len = strlen(s);
for(int j = 0; j < len; j++)
Insert(root, s, j, i);
}
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
scanf("%s", s);
printf("%d\n", Search(root, s));
}
}
HDU 2846 Repository (字典树 后缀建树)的更多相关文章
- 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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository(字典树)
字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- POJ3630/HDU-1671 Phone List,字典树静态建树!
Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
随机推荐
- bzoj2127: happiness(双倍经验最小割)
2127: happiness 题目:传送门 题解: 双倍经验美滋滋~ 请看蒟蒻以前写的渣题解...bzoj3894 表示做完自己就最小割了... 代码(直接改的...菜啊): #include< ...
- wireshark界面调整成英文的
https://ask.wireshark.org/questions/48823/change-the-gui-language 英文版设置 From the Edit (Bearbeiten) m ...
- poj--1789--Truck History(prim)
Truck History Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit ...
- QuerySet和对象的例子 个人记录
import osif __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE&quo ...
- Android属性动画-Interpolator和ViewPropertyAnimator的用法
Interpolator的用法 Interpolator这个东西很难进行翻译,直译过来的话是补间器的意思,它的主要作用是可以控制动画的变化速率,比如去实现一种非线性运动的动画效果.那么什么叫做非线性运 ...
- 51Nod 天堂里的游戏
多年后,每当Noder看到吉普赛人,就会想起那个遥远的下午. Noder躺在草地上漫无目的的张望,二楼的咖啡馆在日光下闪着亮,像是要进化成一颗巨大的咖啡豆.天气稍有些冷,但草还算暖和.不远的地方坐着一 ...
- Python3基础笔记--生成器
目录: 一.列表生成器 二.生成器 三.迭代器 一.列表生成器 a = [x for x in range(10)] b= [y*2 for y in range(10)] def f(n) retu ...
- js数组去重问题
1. 双层循环:外层循环,内层比较值: (1)利用splice直接在原数组进行操作 Array.prototype.delRepeat = function (){ var arr = this; v ...
- Centos7不修改默认交换分区下添加交换分区
交换分区介绍 Linux系统中的交换分区是当物理内存(RAM)被充满时,作为物理内存的缓存来使用. 当系统需要更多的内存资源而物理内存已经充满,内存中不活跃的页就会被移动到交换分区上. 交换分区位于硬 ...
- Intel投入5亿美元提升14nm工艺CPU需求
虽然14nm行将收尾,但是却有大量的客户在赶“末班车”,导致CPU供货告急. Intel年初宣布增加10亿美元的额外资本支出用于转向更新的.更先进的生产工具,以便增加产能,在本周的第39届纳斯达克投资 ...