HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6444 Accepted Submission(s):
2096
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.
(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.
merchandises, whose names contain the search string as their substrings.
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
#include <iostream>
#include<cstring>
#include <cstdio>
#include<string>
#include <algorithm>
using namespace std; typedef struct nn
{
int count;
int id;
nn* nxt[];
}trie; void insert(trie*root, char *s, int id)
{
trie*p = root;
while (*s != '\0')//对字符串的处理还可以这样
{
if (p->nxt[*s - 'a'] == NULL)
{
trie *temp = (trie *)malloc(sizeof(trie));
for (int i = ; i<; i++)
{
temp->nxt[i] = NULL;
}
temp->count = ;
temp->id = -; //-1表示没有商品
p->nxt[*s - 'a'] = temp;
}
p = p->nxt[*s - 'a'];
if (p->id != id)
{//如果当前结点的ID不等于要插入的ID,则计数器count++,并且重新置ID的值
p->id = id;
p->count++;
}
s++;//每一个子串,不如asda里的sd
}
} int search(trie*root, char *s)
{
trie *p = root;
int i;
for (i = ; s[i] != '\0'; i++)
{
if (p->nxt[s[i] - 'a'] == NULL)
return ;
p = p->nxt[s[i] - 'a'];
}
return p->count;
} int main()
{
int i, j;
int n, m;
char s[];
trie *root = (trie*)malloc(sizeof(trie));
for (i = ; i < ; i++)
{
root->nxt[i] = NULL;
}
root->count = ;
root->id = -;
cin >> n;
for (i = ; i <= n; i++)
{
cin >> s;
int l = strlen(s);
for (j = ; j < l; j++)
{//将字符串X=X1X2...Xn的分别以X1,X2...Xn开头的后缀字符串插入到Trie树中
insert(root, s + j, i);
}
}
cin >> m;
for (i = ; i <= m; i++)
{
cin >> s;
cout << search(root, s) << endl;
}
return ;
}
HDU 2846 Repository(字典树,每个子串建树,*s的使用)的更多相关文章
- 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 (字典树)
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(字典树)
字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个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) ...
- hdu2846 Repository 字典树(好题)
把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
- 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 ...
随机推荐
- hdu 4747 mex 线段树+思维
http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意: 我们定义mex(l,r)表示一个序列a[l]....a[r]中没有出现过得最小的非负整数, 然后我 ...
- shell 将输入的字符转换成大写
test.sh #!/bin/bash while read line do echo $line | tr '[a-z]' '[A-Z]' done 输出 dddddd DDDDDD sssss S ...
- [QT]QApplication和QCoreApplication的用法
转自:http://www.tuicool.com/articles/qmI7Bf 故事的背景是这样的,我们在写QT程序的时候或者在开始写QT程序之前总会看到这样的语句 QApplication ap ...
- js中对象的一些特性,JSON,scroll家族
一.js中对象的一些特性 对象的动态特性 1.当对象有这个属性时,会对属性的值重写 2.当对象没有这个属性时,会为对象创建一个新属性,并赋值 获得对象的属性的方式 为元素设置DOM0级事件 二.JSO ...
- git 沙河游戏节点图, 自由沙盒模拟git, 各类交互git命令
git学习练习总资源链接: https://try.github.io/ (练习已通,有document) 本沙盒游戏教学:https://learngitbranching.js.org/?demo ...
- office-word
目录(大纲) word中大纲的视图(也就是目录)是根据1/2/3级大纲决定的 格式刷 可以刷成一样的格式,字体,编号以及大纲等等. 主要用于编号和目录,快捷键(ctrl+shift) 编号设置(不建议 ...
- [转] oracle 监听
oracle 监听 启动监听:lsnrctl start 查看监听:lsnrctl status 停止监听:lsnrctl stop 1.oracle 数据服务器包括:实例进程和数据库: 实例进程包括 ...
- mysql 常见数据类型
---恢复内容开始--- MySQL常见的数据类型 一.数据类型是什么? 数据类型是指列.存储过程参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. 有一些数据是要存储为 ...
- js设计模式整理
单例模式 恶汉式单例 实例化时 懒汉式单例 调用时构造函数模式 1.实现一 function Car(model, year, miles) { this.model = model; this.ye ...
- HDU1501 dfs
像这样有维度的一定要记忆化啊........... #include<cstdio> #include<cstdlib> #include<iostream> #i ...