标签:style blog http color os io java ar for
banana band bee absolute acm ba b band abc
2 3 1 0
解题:trie
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 struct trie{ 18 int wd[27],cnt; 19 trie(){ 20 memset(wd,-1,sizeof(wd)); 21 cnt = 0; 22 } 23 }; 24 trie dic[500000]; 25 int tot = 1; 26 void insertWd(int root,char *s){ 27 for(int i = 0; s[i]; i++){ 28 if(dic[root].wd[s[i]-‘a‘] == -1){ 29 dic[root].wd[s[i]-‘a‘] = tot++; 30 } 31 root = dic[root].wd[s[i]-‘a‘]; 32 dic[root].cnt++; 33 } 34 } 35 int query(int root,char *s){ 36 for(int i = 0; s[i]; i++){ 37 if(dic[root].wd[s[i]-‘a‘] == -1) return 0; 38 root = dic[root].wd[s[i]-‘a‘]; 39 } 40 return dic[root].cnt; 41 } 42 char str[20000]; 43 int main() { 44 int root = 0; 45 while(gets(str) && str[0]) insertWd(root,str); 46 while(gets(str) && str[0]) printf("%d\n",query(root,str)); 47 return 0; 48 }
标签:style blog http color os io java ar for
原文地址:http://www.cnblogs.com/crackpotisback/p/3946469.html