给一些字符串,问以某个串为前缀的串有几个
#include<iostream> #include <algorithm> #include <cstring> using namespace std; const int N=5e5+4; char num[80]; int len,sum[N]; int ch[N][30],tot; int find(char *s){ int i,u=1; for(i=0;i<len;i++){ int c=s[i]-'a'; if(ch[u][c]==0){ return 0; } u=ch[u][c]; } return sum[u]; } void insert(char *s){ int i,u=1; for(i=0;i<len;i++){ int c=s[i]-'a'; if(ch[u][c]==0){ ch[u][c]=++tot; } u=ch[u][c]; sum[u]++; } } int main(){ tot=1; memset(ch,0,sizeof ch); while(gets(num),strlen(num)>0){ len=strlen(num); insert(num); } while(gets(num)){ len=strlen(num); cout<<find(num)<<'\n'; } }
标签:难题,HDU,int,len,char,num,1251,include From: https://www.cnblogs.com/towboa/p/17157065.html