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
0 20 11 11 2
#include <cstdio>
#include <cstring>
char s[25];
int id;
struct node
{
node *next[26];
int cnt;
int id;
node()
{
memset(next, NULL, sizeof(next));
cnt = 0;
id = -1;
}
};
void Insert(node *p, char *s, int index, int id)
{
for(int i = index; s[i] != '\0'; i++)
{
int idx = s[i] - 'a';
if(p -> next[idx] == NULL)
p -> next[idx] = new node();
p = p -> next[idx];
if(p -> id != id)
{
p -> id = id;
p -> cnt ++;
}
}
}
int Search(node *p, char *s)
{
for(int i = 0; s[i] != '\0'; i++)
{
int idx = s[i] - 'a';
if(p -> next[idx] == NULL)
return 0;
p = p -> next[idx];
}
return p -> cnt;
}
int main()
{
int p, q;
id = 0;
node *root = new node();
scanf("%d", &p);
for(int i = 0; i < p; i++)
{
scanf("%s", s);
int len = strlen(s);
for(int j = 0; j < len; j++)
Insert(root, s, j, i);
}
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
scanf("%s", s);
printf("%d\n", Search(root, s));
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 2846 Repository (字典树 后缀建树)
原文地址:http://blog.csdn.net/tc_to_top/article/details/46855063