题意:
给一组单词 开始提问每次给一个串求该串是上面几个单词的前缀
分析:
没给数据规模,但用链表写ME好几次,又用数组写开小RE了,试了几次才过了,真是醉了。。。
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef pair<int,int> PII; typedef long long ll; #define lson l,m,rt<<1 #define pi acos(-1.0) #define rson m+1,r,rt<<11 #define All 1,N,1 #define read freopen("in.txt", "r", stdin) #define N 500100 const ll INFll = 0x3f3f3f3f3f3f3f3fLL; const int INF= 0x7ffffff; const int mod = 1000000007; struct Trie{ int ch[N][26]; int val[N],num; void init(){ memset(ch[0],0,sizeof(ch[0])); memset(val,0,sizeof(val)); num=1; } void build(char *s){ int u=0,len=strlen(s); for(int i=0;i<len;++i){ int v=s[i]-‘a‘; if(!ch[u][v]){ memset(ch[num],0,sizeof(ch[num])); ch[u][v]=num++; } u=ch[u][v]; val[u]++; } } int query(char *s){ int u=0,len=strlen(s); for(int i=0;i<len;++i){ int v=s[i]-‘a‘; if(!ch[u][v])return 0; u=ch[u][v]; } return val[u]; } }trie; int main() { char str1[20],str2[20]; trie.init(); while(gets(str1)&&str1[0]!=‘\0‘) trie.build(str1); while(scanf("%s",str2)!=EOF){ printf("%d\n",trie.query(str2)); } return 0; }
时间: 2024-11-02 23:23:43