标签:des style class blog code java
banana band bee absolute acm ba b band abc
2 3 1 0
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> using namespace std; const int maxn = 500000; int ch[maxn][26]; int cnt; int val[maxn]; inline int getIdx(char a){ return a-'a'; } void init(){ memset(ch[0],0,sizeof ch[0]); cnt = 1; memset(val,0,sizeof val); } void insert(string st){ int u = 0; for(int i = 0; i < st.size(); i++){ int k = getIdx(st[i]); if(!ch[u][k]){ ch[u][k] = cnt; val[cnt]++; cnt++; }else{ val[ch[u][k]]++; } u = ch[u][k]; } } int query(string st){ int u = 0; for(int i = 0; i < st.size(); i++){ int k = getIdx(st[i]); if(!ch[u][k]) return 0; u = ch[u][k]; } return val[u]; } int main(){ string st; init(); while(getline(cin,st)&&st.size()){ insert(st); } while(cin >> st){ cout<<query(st)<<endl; } return 0; }
HDOJ1251-统计难题(trie树入门),布布扣,bubuko.com
标签:des style class blog code java
原文地址:http://blog.csdn.net/mowayao/article/details/32340263