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

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 ;
}
上一篇:MTK平台Android项目APK预置方案


下一篇:打包spring项目遇到的坑 Unable to locate Spring NamespaceHandler for XML schema ……shcema/context 产生的原因及解决方法