传送门:hihocoder 1014
给出n个字符串,再给m次询问,每次给出一个字符串,问给定的n个字符串中,以该字符串为开头的字符串有多少个。
就像题目名字一样,标标准准的trie树,在建树的过程中统计每个点被访问了多少次即可
/******************************************************
* File Name: 1014.cpp
* Author: kojimai
* Create Time: 2014年11月08日 星期六 14时14分52秒
******************************************************/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define FFF 100005
struct node
{
int cnt;
node *next[26];
};
node* newnode()
{
node *p = (node *)malloc(sizeof(node));
for(int i = 0;i < 26;i++)
{
p->next[i] = NULL;
}
p->cnt = 0;
return p;
}
void build(char *s,node *root)
{
node *p = root;
for(int i = 0;s[i] != '\0';i++)
{
int t = s[i] - 'a';
if(p -> next[t] == NULL)
{
node *q = newnode();
p -> next[t] = q;
}
p = p -> next[t];
p -> cnt++;
}
return;
}
int solve(char *s,node *root)
{
node *p = root;
for(int i = 0;s[i] != '\0';i++)
{
int t = s[i] - 'a';
if(p -> next[t] == NULL)
return 0;
else
p = p -> next[t];
}
return p->cnt;
}
int main()
{
int n,m;
char s[23];
scanf("%d",&n);
node* root = newnode();
for(int i = 0;i < n;i++)
{
scanf("%s",s);
build(s,root);
}
scanf("%d",&m);
for(int i = 0;i < n;i++)
{
scanf("%s",s);
cout<<solve(s,root)<<endl;
}
return 0;
}