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 ...
随机推荐
- 如何优雅地发布Hexo博客
前言 就目前而言,我所知道的发布Hexo的博客有如下几种: 1.原始方式,也就是在服务器上编写md文件,然后利用hexo g来生成,详见:hexo从零开始到搭建完整: 2.利用github+hook来 ...
- 雷林鹏分享:Ruby 异常
Ruby 异常 异常和执行总是被联系在一起.如果您打开一个不存在的文件,且没有恰当地处理这种情况,那么您的程序则被认为是低质量的. 如果异常发生,则程序停止.异常用于处理各种类型的错误,这些错误可能在 ...
- sed:轻量级流编辑器
一. sed命令 sed是一种几乎包括在所有UNIX平台(包括Linux)的轻量级流编辑器.sed主要是用来将数据进行选取.替换.删除.新增的命令 注意:vi命令只能修改文件,但不能修改命令的结果,如 ...
- hdu1847sg函数
刚开始因为没注意到f是从0开始的导致wa了几次,f遍历的时候从0到f[j]<=i 这个题只有一个子情况,求出sg值直接判断就好了 #include<map> #include< ...
- HIVE之正则化详解
有大神写的很好了,我借花献佛,有兴趣,看链接,在此不再赘述.想要学习Hive正则表达式重点应该是正则表达式的表示方式,只有正则表达式使用溜了,hive正则那就是小case. 附参考博文: https: ...
- Oracle 9i & 10g编程艺术-深入数据库体系结构-学习笔记(持续更新中)
--20170322 --1.0 --更新表的统计信息begin dbms_stats.set_table_stats(user,'EMP',numrows => 10000);end; beg ...
- OleDbCommand使用参数应该注意的地方
最近写程序用到OleDbCommand的Parameter写数据库,遇到很多问题: 1.OLE DB .NET Framework 数据提供程序和 ODBC .NET Framework 数据提供程序 ...
- 个人知识管理系统Version1.0开发记录(12)
最近碰到个问题,在五个工作日内阅读一个百万行左右代码量的新项目集合,如何解决呢? 第一个工作日,环境观察.待在那个项目组,看项目成员们在做些什么事情,开发,测试,聊天,或多或少可以收集到一些项目相关的 ...
- javascript: 数据类型深入理解
1.基本类型(值类型或者原始类型): Number.Boolean.String.NULL.Undefined以及ES6的Symbol2.引用类型:Object.Array.Function.Date ...
- L180
The cylinder is a crucial part of the washing machine His debonair dismissal of my inquiry concernin ...