题目大意:
给n个匹配串.然后给m个模式串,查询它们作为前缀在匹配串中出现的次数.
思路: Trie树
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
namespace Trie{
int tr[maxn][27] , v[maxn] , tot;
void init ()
{
tot = 0;
for(int i = 0 ; i < maxn ; i++) {
for(int j = 0 ; j < 26 ; j++) tr[i][j] = 0;
v[i] = 0;
}
}
void add (char * s){
int u = 0;v[u]++;
for (int i = 1 ; s[i] ; i++){
if (!tr[u][s[i] - 'a']) tr[u][s[i] - 'a'] = ++tot;
u = tr[u][s[i] - 'a'];
v[u]++;
}
}
int ask (char * s){
int u = 0;
for(int i = 1 ; s[i] ; i++){
if (!tr[u][s[i] - 'a']) return 0;
u = tr[u][s[i] - 'a'];
}
return v[u];
}
}
char tmp[15];
int main()
{
ios::sync_with_stdio(false);
while (1){
cin.getline(tmp + 1, 11);
if (strlen(tmp + 1) == 0) break;
Trie::add(tmp);
}
while(cin >> (tmp + 1)){
cout << Trie::ask(tmp) << endl;
}
return 0;
}