标签:title printf get ssi tput ace stdin nat blank
banana band bee absolute acm ba b band abc
2 3 1 0
解析:Trie树模板。
AC代码:
#include <bits/stdc++.h> using namespace std; const int maxnode = 100000 * 10 + 5; const int sigma_size = 26; struct Trie{ int ch[maxnode][sigma_size]; int val[maxnode]; int sz; void clear(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); } //初始仅仅有一个根节点 int idx(char c){ return c - ‘a‘; } void insert(string s){ int u = 0, n = s.size(); 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 find(string s){ int u = 0, n = s.size(); 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]; } }; Trie T; //临时还不知道为什么把它放到main里面就不行,拿出来就好了 int main(){ #ifdef sxk freopen("in.txt", "r", stdin); #endif // sxk string s; T.clear(); while(getline(cin, s)){ if(s == "") break; T.insert(s); } while(getline(cin, s)){ printf("%d\n", T.find(s)); } return 0; }
标签:title printf get ssi tput ace stdin nat blank
原文地址:http://www.cnblogs.com/cxchanpin/p/6766791.html