hdu 2846 Repository 字典树的变形

简介:

Repository

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


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
 
根据题意可知:题目属于判断字符串中是否包含子串的问题,对于一般的字典树,用来判断前缀,而这里不能直接这么去建树。在建树的时候将字符串X=X1X2....Xn的分别以X1,X2....Xn开头的后缀子串插入到Trie树中,如此一来就可以判断某个字符串是否被包含在另一个字符串当中。但是这就有一个问题,比如插入了字符串abab,那么当查找字符串ab时就会重复计数,因此需要多设计一个标识以表示在插入"abab"和"ab"时时同一个字符串即可(是同一个字符串就不需要使计数器加1),因此在Trie树结点中多设计一个商品id来标记。id用来记录最后一个经过此路径上的商品编号,如果要插入的字符串编号同当前节点的编号不同,则计数器加1,并且将当前结点的编号置为要插入的字符串的编号。
复制代码
/*hdu 2846 字典树的变形 2011.10.18*/ 
#include <iostream>
#include<cstring>
#define MAX 26
using namespace std;

typedef struct Trie_Node
{
int count; //记录包含该结点的单词个数
int id; //最后一次经过此结点的商品ID
Trie_Node *next[MAX];
}Trie;

void insert(Trie *root,char *s,int id)
{
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
Trie *temp=(Trie *)malloc(sizeof(Trie));
for(int i=0;i<MAX;i++)
{
temp->next[i]=NULL;
}
temp->count=0;
temp->id=-1; //-1表示没有商品
p->next[*s-'a']=temp;
}
p=p->next[*s-'a'];
if(p->id!=id) //如果当前结点的商品ID不等于要插入商品的ID,则计数器count++,并且重新置ID的值
{
p->id=id;
p->count++;
}
s++;
}
}


int search(Trie *root,char *s)
{
Trie *p=root;
for(int i=0;s[i]!='\0';i++)
{
if(p->next[s[i]-'a']==NULL)
return 0;
p=p->next[s[i]-'a'];
}
return p->count;
}

int main(int argc, char *argv[])
{
int i,j;
int n,m;
char s[21];
Trie *root=(Trie *)malloc(sizeof(Trie));
for(i=0;i<MAX;i++)
{
root->next[i]=NULL;
}
root->count=0;
root->id=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",s);
for(j=0;j<strlen(s);j++) //将字符串X=X1X2...Xn的分别以X1,X2...Xn开头的后缀字符串插入到Trie树中
{
insert(root,s+j,i);
}
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%s",s);
printf("%d\n",search(root,s));
}
return 0;
}
复制代码
 尝试过用KMP去解决,但是查找复杂度至少为0(p*(len1+len2)),试着提交了一下,严重超时。

本文转载自海 子博客园博客,原文链接:http://www.cnblogs.com/dolphin0520/archive/2011/10/18/2216208.html 如需转载自行联系原作者




相关文章
HDU7018.Banzhuan(计算几何+贪心)
HDU7018.Banzhuan(计算几何+贪心)
110 0
HDU7018.Banzhuan(计算几何+贪心)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)
97 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
大体思路: 二分,将原矩阵根据二分的值变成01矩阵,如果元素值> val 就变为1,否则0 对于k * k 的矩阵,统计区域内元素之和,如果 sum < ⌊k2 / 2⌋ + 1,意味着当前k * k矩阵的中位数小于x,而x是我们的答案(最小中位数), ①sum < ⌊k2 / 2⌋ + 1 情况下x取得太大,r = mid ②反之,x还可能取更小的,l = mid 但是需要注意下l的初始值,当取0 or 1的时候是会wa掉的:
254 0
AtCoder Beginner Contest 203 Pond(二分+二维前缀和)
|
人工智能 BI Go
[Atcoder ABC222] F - Expensive Expense | 妙用树的直径 | Dijkstra
Problem Statement The Kingdom of AtCoder is composed of N towns and N−1 roads. The towns are labeled as Town 1, Town 2, …, Town N. Similarly, the roads are labeled as Road 1, Road 2, …, Road N−1. Road i connects Towns Ai and Bi bidirectionally, and you have to pay the toll of Ci to go through it.
220 0
[Atcoder ABC222] F - Expensive Expense | 妙用树的直径 | Dijkstra
|
人工智能 安全
UPC——校门内的树—>二分
题目描述 FZYZ 大门的左侧有一排 n 棵树木。它们按照距离的远近排列,第 1 棵树的高度为 a1 米,第 2 棵树木的高度为 a2 米,第 3 棵树木的高度为 a3 米,……,第 n 棵树木的高度为 an米。 为了给同学们以积极向上的感觉,一些同学自发地决定对树木进行修剪,使得树木呈现上升的趋势。具体地说,他们希望对树木进行修剪和整理,使得修剪之后的树木高度 b1,b2,b3,…,bn 米且满足 b1<b2<b3<…<bn。
136 0
|
搜索推荐
HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
89 0

热门文章

最新文章