标签:
1、HDU 1251 统计难题 Trie树模板题,或者map
2、总结:用C++过了,G++就爆内存。。
题意:查找给定前缀的单词数量。
#include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b) for(int i=a;i<=b;i++) #define mes(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define LL long long using namespace std; const int N=10010,MAX=1000100; struct Node { int count; Node *child[26]; Node(){ mes(child,NULL); count=0; } }; Node *root=new Node,*current; void insert(char *str) { current=root; for(int i=0;str[i];++i){ int m=str[i]-‘a‘; if(current->child[m]==NULL){ current->child[m]=new Node; } current=current->child[m]; current->count++; } } int search(char *str) { current=root; for(int i=0;str[i];++i){ int m=str[i]-‘a‘; if(current->child[m]==NULL) return 0; current=current->child[m]; } return current->count; } int main() { char str[20]; while(gets(str),*str) insert(str); while(gets(str)) printf("%d\n",search(str)); return 0; }
标签:
原文地址:http://www.cnblogs.com/sbfhy/p/5877426.html