HDU 1251 ——统计难题(Trie 入门)
来源:互联网 发布:神泣单机数据库 编辑:程序博客网 时间:2024/06/06 00:10
思路就是套模板。。。。
弱菜学指针ing
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#define maxnode 10000#define sigma_size 26using namespace std;struct tree{ int num; struct tree* next[sigma_size];};struct tree *root;void insert(char *s){ struct tree *p=root,*q; int l=strlen(s); for(int i=0;i<l;++i){ int c=s[i]-'a'; if(p->next[c]==NULL){ q=(struct tree* )malloc(sizeof(struct tree)); q->num=1; for(int j=0;j<sigma_size;++j){ q->next[j]=NULL; } p->next[c]=q; p=p->next[c]; } else{ p=p->next[c]; p->num++; } }}int query(char *s){ struct tree *p=root; int l=strlen(s); for(int i=0 ;i<l;++i){ int c=s[i]-'a'; p=p->next[c]; if(p==NULL) return 0; } return p->num;}int main(){ char str[11]; root=(struct tree* )malloc(sizeof(struct tree)); for(int i=0;i<sigma_size;++i){ root->next[i]=NULL; } root->num=0; while(gets(str)&&strcmp(str,"")){ insert(str); } while(scanf("%s",str)!=EOF){ printf("%d\n",query(str)); } return 0;}
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#define maxnode 1000#define sigma_size 26using namespace std;struct tree{ int num; struct tree *next[sigma_size];};tree root;void insert(char *s){ tree *p=&root,*q; int l=strlen(s); for(int i=0;i<l;++i){ int c=s[i]-'a'; if(p->next[c]==NULL){ q=(tree *)malloc(sizeof(tree)); (*q).num=1; for(int j=0;j<sigma_size;++j){ q->next[j]=NULL; } p->next[c]=q; } else{ p->next[c]->num++; } p=p->next[c]; }}int query(char *s){ tree *p=&root,*q; int l=strlen(s); for(int i=0;i<l;++i){ int c=s[i]-'a'; p=p->next[c]; if(p==NULL) return 0; } return p->num;}int main(){ char str[11]; for(int i=0;i<26;++i){ root.next[i]=NULL; } while(gets(str)&&strcmp(str,"")){ insert(str); } while(scanf("%s",str)!=EOF){ printf("%d\n",query(str)); } return 0;}
#include<iostream>#include<cstdio>#include<cstring>#define maxnode 500000#define sigma_size 26using namespace std;struct Trie{ int ch[maxnode][sigma_size]; int sz; int cont[maxnode]; Trie() {memset(ch,0,sizeof ch);memset(cont,0,sizeof cont);sz=1;} int idx(char c) {return c-'a';} void insert(char *s) { int u=0,l=strlen(s); for(int i=0;i<l;++i){ int c=idx(s[i]); if(!ch[u][c]){ ch[u][c]=sz++; } u=ch[u][c]; cont[u]++; //字符串的最后一个节点记录信息 } } int query(char *s) { int u=0,l=strlen(s); for(int i=0;i<l;++i){ int c=idx(s[i]); if(!ch[u][c]) return 0; u=ch[u][c]; } return cont[u]; }}T;int main(){ char str[12]; while(gets(str), *str){ T.insert(str); } while(scanf("%s",str)!=EOF){ printf("%d\n",T.query(str)); } return 0;}
0 0
- HDU 1251 ——统计难题(Trie 入门)
- HDU 1251 统计难题 // Trie入门题
- HDU 1251 统计难题 字典树入门(Trie)
- HDU 1251 统计难题(trie树入门)
- hdu 1251 统计难题(trie树)
- HDU 1251 - 统计难题 (Trie)
- HDU 1251 统计难题 (Trie)
- hdu 1251 统计难题 (trie树)
- hdu 1251统计难题(trie)
- hdu 1251 统计难题 Trie
- hdu 1251 统计难题(trie)
- hdu 1251 统计难题 trie
- HDU-1251 统计难题 (Trie)
- HDU 1251 统计难题 (Trie树——字符串算法)
- HDU 1251 统计难题 (字符串-Trie树)
- (字典树Trie Tree)HDU 1251 统计难题
- hdu 1251 统计难题//trie树
- hdu 1251 统计难题(Trie 水题)
- Spring数据源配置的三种方式
- git permisson denied
- 【.NET 】ASP.NET中Response.End()异常
- 中缀、前缀和后缀表达式求值问题
- DWR与js的同步异步问题
- HDU 1251 ——统计难题(Trie 入门)
- oracle11g创建表空间
- 【.NET】 DictionaryCloneable 字典类Dictionary复制
- 有向图的强连通分支及DAG
- CentOs service 管理
- UVA 10341 Solve It
- 高性能IO模式--Reactor和Proactor
- Trie模板
- document.body.scrollTop和document.documentElement.scrollTop在不同浏览器下的问题