标签:absolute hdu mem script comm 长度 cstring class size
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 36643 Accepted Submission(s):
13626
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; char s[11]; bool p; struct node { int count; node * next[26]; }*root; node * build() { node * k=new(node); k->count=0; memset(k->next,0,sizeof(k->next)); return k; } void insert() { node * r=root; char * word=s; while(*word) { int id=*word-‘a‘; if(r->next[id]==NULL) r->next[id]=build(); r=r->next[id]; r->count++; word++; } } int search() { node * r=root; char * word=s; while(*word) { int id=*word-‘a‘; r=r->next[id]; if(r==NULL) return 0; word++; } return r->count; } int main() { root=build(); while(gets(s)) { if(!p) { if(strlen(s)) insert(); else p=true; } else printf("%d\n",search()); } }
2、数组模拟指针写法 时间 46ms 空间:40892k
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int trie[400001][26],len,root,tot,sum[400001]; bool p; char s[11]; void insert() { len=strlen(s); root=0; for(int i=0;i<len;i++) { int id=s[i]-‘a‘; if(!trie[root][id]) trie[root][id]=++tot; sum[trie[root][id]]++; root=trie[root][id]; } } int search() { root=0; len=strlen(s); for(int i=0;i<len;i++) { int id=s[i]-‘a‘; if(!trie[root][id]) return 0; root=trie[root][id]; } return sum[root]; } int main() { while(gets(s)) { if(!p) { if(strlen(s)) insert(); else p=1; } else printf("%d\n",search()); } }
标签:absolute hdu mem script comm 长度 cstring class size
原文地址:http://www.cnblogs.com/TheRoadToTheGold/p/6289062.html