标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 21814 Accepted Submission(s): 9297
/** 题意:如题 做法:trie树 G++ MLE了n次 C++ AC 与AC自动机建树相同 **/ #include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <cmath> using namespace std; struct node { int Count; node *next[26]; node() { memset(next,NULL,sizeof(next)); Count = 0; } }; node *p,*root = new node(); void Insert(char *s) { int i,k; for(p = root,i=0; s[i]; ++i) { k = s[i] -‘a‘; if(p->next[k] == NULL) p->next[k] = new node(); p = p->next[k]; p->Count++; } } int Search(char *s) { int i,k; for(p = root,i=0; s[i]; i++) { k = s[i] -‘a‘; if(p->next[k] == NULL) return 0; p = p->next[k]; } return p->Count; } int main() { //#ifndef ONLINE_JUDGE // freopen("in.txt","r",stdin); //#endif char s[20]; root = new node(); while(1) { cin.getline(s,20); if(strcmp(s,"") == 0) break; Insert(s); } int tt = 0; while(1) { cin.getline(s,20); if(strcmp(s,"") == 0) break; printf("%d\n",Search(s)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/chenyang920/p/4589788.html