Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6444    Accepted Submission(s):
2096

Problem Description
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.
 
Input
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.
 
Output
For each query, you just output the number of the
merchandises, whose names contain the search string as their substrings.
 
Sample Input
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
 
Sample Output
0
20
11
11
2
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar
problems for you:  2852 2847 2845 2850 2851 
 
根据题意可知:题目属于判断字符串中是否包含子串的问题,对于一般的字典树,用来判断前缀,而这里不能直接这么去建树。在建树的时候将字符串X=X1X2....Xn的分别以X1,X2....Xn开头的后缀子串插入到Trie树中,如此一来就可以判断某个字符串是否被包含在另一个字符串当中。但是这就有一个问题,比如插入了字符串abab,那么当查找字符串ab时就会重复计数,因此需要多设计一个标识以表示在插入"abab"和"ab"时时同一个字符串即可(是同一个字符串就不需要使计数器加1),因此在Trie树结点中多设计一个商品id来标记。id用来记录最后一个经过此路径上的商品编号,如果要插入的字符串编号同当前节点的编号不同,则计数器加1,并且将当前结点的编号置为要插入的字符串的编号。
 
 
经验:对字符串的处理还可以这样insert(root, s + j, i);,void insert(trie*root, char *s, int id),*s就是某个s[i],s++就相当于i++
 #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的使用)的更多相关文章

  1. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  3. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. HDU 2846 Repository(字典树)

    字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...

  6. hdu 2846 Repository

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others)     ...

  7. hdu2846 Repository 字典树(好题)

    把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...

  8. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  9. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

随机推荐

  1. CentOS6安装Pyhon3

    一,从官方下载Python3.6 [root@linux-node1 src]# pwd /usr/local/src [root@linux-node1 src]# wget https://www ...

  2. 基于Oracle Sequence的流水号生成规则

    流水号在各种系统中随处可见,一般都是使用自增.年月日时分秒+自增.UUID等,要么纯数字,要么纯字母,这种流水号缺乏一定的辨识度. 下面为大家介绍一种具有辨识度的流水号的生成方式:领域或者应用的标识 ...

  3. QT 正则表达式无效

    背景:写了一个判断IP地址合法的正则表达式,并让它应用在输入框中 代码如下 QRegExp rx_ip("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}( ...

  4. Java中的组合与聚合

    组合和聚合是有很大区别的,这个区别不是在形式上,而是在本质上:比如A类中包含B类的一个引用b,当A类的一个对象消亡时,b这个引用所指向的对象也同时消亡(没有任何一个引用指向它,成了垃圾对象),这种情况 ...

  5. ros 杀掉所有节点

    rosnode kill -a 或者 rosnode kill --all

  6. YAML(摘录)

    YAML:维基百科 一个用来表达数据序列的格式.强调以数据为中心的标记语言. 使用空白符缩进和大量依赖外观的特殊,适合编辑数据结构,配置文件. 基本格式: 缩进/区块 和内置两者格式,来表示array ...

  7. phpstorm 2017版代码提示功能开启解决方案

    安装好phpstorm 2017之后 发现代码高亮和函数自动提示都失效了 在phpstorm底部面板的信息提示处发现有一条系统消息: 12:04:18 Power save mode is on Co ...

  8. Android之第三方平台实现多平台分享操作

    开发中常常遇到分享操作,当用到多种分享时,如:QQ,微信,微博,短信等,可以借助第三方平台来完成,此博客主要借助mob平台来完成相关操作,当然也可以借助其他平台,如友盟等. 先来看看效果图: 如图看出 ...

  9. Struts2基本使用(一)--在项目中引入Struts2

    Struts2基本使用 在MVC开发模式中,Struts2充当控制器(Controller)的角色.其主要功能就是处理用户请求,生成响应,是连接视图层(View)和模型层(Model)的桥梁.在处理用 ...

  10. ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program create ...