问题
hdu 1251 统计难题 - https://acm.hdu.edu.cn/showproblem.php?pid=1251
分析
- trie[0]作为根
代码
/* hdu 1251 统计难题 */
#include<bits/stdc++.h>
using namespace std;
const int MXN = 500010;
int trie[MXN][26], num[MXN], tot = 0, len;
void insert(char s[]){
for(int p = 0, i = 0; i < len; ++i){
if(trie[p][s[i]-'a'] == 0) trie[p][s[i]-'a'] = ++tot;
p = trie[p][s[i]-'a'], ++num[p];
}
}
int query(char s[]){
int p = 0;
for(int i = 0; i < len; ++i){
if(trie[p][s[i]-'a'] == 0) return 0;
p = trie[p][s[i]-'a'];
}
return num[p];
}
int main(){
char s[11];
memset(trie[0], 0, sizeof trie[0]);
while(gets(s) && (len = strlen(s))) insert(s);
while(gets(s) && (len = strlen(s))) printf("%d\n", query(s));
return 0;
}