标签:模板 %s span col iostream color str bsp pre
题意:
给你一堆单词与询问,每次询问给一个字符串s问以s为前缀的字符串有多少
思路:
#include<iostream> #include<algorithm> #include<cstring> using namespace std; const int maxn=400009; struct tire{ int ch[maxn][26]; int val[maxn]; int sz; void init(){ sz=1; memset(ch[0],0,sizeof(ch[0])); val[0]=0; } int idx(char c){return c-‘a‘;} void insert(char *s){ int u=0; int n=strlen(s); for(int i=0;i<n;i++){ int c=idx(s[i]); if(!ch[u][c]){ memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } val[u]++; u=ch[u][c]; } val[u]++; } int query(char *s){ int u=0; int n=strlen(s); for(int i=0;i<n;i++){ int c=idx(s[i]); if(!ch[u][c]) return 0; u=ch[u][c]; } return val[u]; } }tire; int main() { char s[20]; tire.init(); while(gets(s)&&s[0]!=0) tire.insert(s); while(scanf("%s",&s)!=EOF) printf("%d\n",tire.query(s)); return 0; }
标签:模板 %s span col iostream color str bsp pre
原文地址:https://www.cnblogs.com/overrate-wsj/p/12257582.html