前缀统计

前缀统计_第1张图片
题解:
把这个N个字符串插入一棵Trie树,Trie树上的每个节点上存储一个整数cnt,记录该节点是多少个字符串的末尾节点(为了处理插入重复字符串的情况,这里要记录个数,而不能只做结尾标记)
对于每个询问,在Trie树中检索T,在检索过程中累加途径的每个节点的cnt值,就是该询问的答案.

#include 
using namespace std;
const int maxn=2e6+5;

int trie[maxn][26],tot;
int sum[maxn];
char ss[maxn];

void Insert(char *s){
    int root=0,len=strlen(s);
    for(int i=0;i

你可能感兴趣的:(字典树Trie)