题意
中文题:http://hihocoder.com/problemset/problem/1260
思路
- 建一个字典树,暴力dfs求解每个提问
实现
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstdlib>
#include <assert.h>
#include <string>
#include <set>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 100005;
const int maxm = 10004;
int n,m;
struct Node{
char ch;
bool mark;
}node[maxn];
vector<int> g[maxn];
char str[maxn];
int num = 1;
void add(int len){
int now = 0;
for (int i=0;i<len;i++){
int flag = 0;
for (int j=0;j<g[now].size();j++){
if (node[g[now][j]].ch == str[i]){
flag = 1;
now = g[now][j];
break;
}
}
if (flag == 1)
continue;
node[num].ch = str[i];
node[num].mark = 0;
g[now].pb(num);
now = num;
num++;
}
node[now].mark = 1;
}
int dfs(int u,int dep,int mark){
if (dep == strlen(str) + 1 ){
if (node[u].mark == 1)
return 1;
else
return 0;
}
int ret = 0;
for (int i=0;i<g[u].size();i++){
if (node[g[u][i]].ch != str[dep-mark]){
if (mark == 1){
continue;
}
else{
ret += dfs(g[u][i],dep+1,mark+1);
}
}
else{
ret += dfs(g[u][i],dep+1,mark);
}
}
return ret;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++){
scanf("%s",str);
add(strlen(str));
}
for (int i=0;i<m;i++){
scanf("%s",str);
printf("%d\n",dfs(0,0,0));
}
return 0;
}