标签:content queue others 12px bottom desc pre ott gre
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 37626 Accepted Submission(s): 13858
#include <iostream> #include <cstring> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define lson(x) ((x<<1)) #define rson(x) ((x<<1)+1) using namespace std; typedef long long ll; const int N=26; const int M=1e6+10; typedef struct TrieNode { int nCount; struct TrieNode *next[N]; } TrieNode; TrieNode Memory[M]; int allocp=0; void InitTrieRoot(TrieNode **pRoot) { *pRoot=NULL; } TrieNode *CreateTrieNode() { TrieNode *p; p=&Memory[allocp++]; p->nCount=1; for(int i=1; i<N; i++) { p->next[i]=NULL; } return p; } void InsertTrie(TrieNode **pRoot,char *s) { int i,k; TrieNode *p; if(!(p=*pRoot)) p=*pRoot=CreateTrieNode(); i=0; while(s[i]) { k=s[i++]-‘a‘; if(p->next[k]) p->next[k]->nCount++; else p->next[k]=CreateTrieNode(); p=p->next[k]; } } int SearchTrie(TrieNode **pRoot,char *s) { TrieNode *p; int i,k; if(!(p=*pRoot)) return 0; i=0; while(s[i]) { k=s[i++]-‘a‘; if(p->next[k]==NULL) return 0; p=p->next[k]; } return p->nCount; } int main() { char str[11]; TrieNode *Root=NULL; InitTrieRoot(&Root); while(gets(str)&&str[0]) { InsertTrie(&Root,str); } while(gets(str)) { printf("%d\n",SearchTrie(&Root,str)); } return 0; }
标签:content queue others 12px bottom desc pre ott gre
原文地址:http://www.cnblogs.com/jianrenfang/p/6384012.html