hdu 2846 Repository (字典树)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7233 Accepted Submission(s): 2278
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.
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.
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
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
C/C++:
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = 1e6 + ; struct node
{
int id, cnt, next[];
}my_node[my_max];
int flag = ; void my_init(int x)
{
for (int i = ; i < ; ++ i)
my_node[x].next[i] = -;
} void my_insert(char *s, int len, int x)
{
int now = ;
for (int i = ; i < len; ++ i)
{
if (my_node[now].next[s[i] - 'a'] == -)
{
my_node[now].next[s[i] - 'a'] = ++ flag;
now = flag;
my_init(flag);
}
else now = my_node[now].next[s[i] - 'a'];
if (my_node[now].id != x) my_node[now].cnt ++;
my_node[now].id = x;
}
} int my_find(char *s)
{
int now = , len = strlen(s);
for (int i = ; i < len; ++ i)
{
if (my_node[now].next[s[i] - 'a'] == -) return ;
now = my_node[now].next[s[i] - 'a'];
}
return my_node[now].cnt;
} int main()
{
int n, len;
scanf("%d", &n);
char s[];
my_init();
while (n --)
{
scanf("%s", s);
len = strlen(s);
for (int i = ; i < len; ++ i)
my_insert(s + i, len - i, n + );
}
scanf("%d", &n);
while (n --)
{
scanf("%s", s);
printf("%d\n", my_find(s));
}
return ;
}
hdu 2846 Repository (字典树)的更多相关文章
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- 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) ...
- 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 ...
- hdu2846 Repository 字典树(好题)
把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
随机推荐
- css定位 双飞翼
<!doctype html><html><head><meta charset="utf-8"><title>双飞翼& ...
- 使用Jersey构建图片服务器
使用Jersey构建图片服务器 前台页面代码 <form id="jvForm" action="add.do" method="post&qu ...
- JVM 垃圾收集与内存分配
判断对象是否还活着 引用计数法 给对象添加引用计数器,添加加1,引用失效减1,如果为0就是不可使用的.问题是不能解决互相引用带来的问题 可达性分析法 以GC Roots为起点,判断到一个对象是否有引用 ...
- solr学习篇(二) solr 分词器篇
关于solr7.4搭建与配置可以参考 solr7.4 安装配置篇 在这里我们探讨一下分词的配置 目录 关于分词 配置分词 验证成功 1.关于分词 1.分词是指将一个中文词语拆成若干个词,提供搜索引擎 ...
- LeetCode刷题笔记(6)按照索引计算int[] 数组中的和([Time Limit Exceeded]问题)
Easy303 Easy633 package easy; public class e303 { private int[] sums; public e303(int[] nums) { sums ...
- django-URL默认参数传递
主要用在分页中. book/views.py def page(request,pn=): return HttpResponse("<h1>{}</h1>" ...
- quartus使用串口IP模块
在quartus平台中使用串口模块的IP,需要使用到platform designer软件来实现. 1.在quartus界面调出IP Catalog界面. 2.在IP catalog中搜索UART,找 ...
- bit(比特)与Byte(字节)的区别与关系
1.bit:位 (小写b) 也称比特 是英文 binary digit的缩写 二进制数系统中,每个0或1就是一个位(bit)位是数据存储(计算机中信息)的最小单位计算机中的CPU位数指的是CPU一次能 ...
- tomcat无法启动的原因
一.排查思路 最直接也是最有效的办法:看console控制台 这是我看到的原因,我先想到是不是web.xml里的url-pattern里的命名是不是冲突 因为我在这个项目之前写了一个项目,用的是同一个 ...
- python实现清屏
往常都是用os.system("cls")清屏,但是发现每次执行完这个命令后都会出现一个空白字符 尝试了一下午,网上也没解决的办法 最后: os.system("cls& ...