Trie树
来源:互联网 发布:淘宝pos机 编辑:程序博客网 时间:2024/06/08 07:06
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int maxnode=4000*100+10;const int MAXN=300;const int MAXM=1000;const int sigma_size=26;struct Trie{ int ch[maxnode][sigma_size]; int val[maxnode]; int sz;//结点总数 Trie(){sz=1;memset(ch[0],0,sizeof(ch[0]));}//初始只有一个根节点 int idx(char c) {return c-'a';}//字符c的编号 //插入字符串s,附加信息为v。注意v必须非0,因为0代表”本结点不是单词结点“ void Insert(char *s,int v) { int u=0,n=strlen(s); for(int i=0;i<n;++i) { int c=idx(s[i]); if(!ch[u][c])//结点不存在 { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0;//中间结点的附加信息为0 ch[u][c]=sz++;//新建结点 } u=ch[u][c];//往下走 } val[u]=v;//字符串的最后一个字符的附加信息为v } int query(char *s) { int len=strlen(s); int u=0; for(int i=0;i<len;++i) { int id=idx(s[i]); if(ch[u][id]==0) return 0; u=ch[u][id]; } return val[u]; }};Trie trie;char word[MAXN];char text[MAXM];int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;++i) { scanf("%s",word); trie.Insert(word,i); } for(int i=1;i<=m;++i) { scanf("%s",word); printf("%d\n",trie.query(word)); } } return 0;}
阅读全文
0 0
- hihoCoder1014 Trie树 [Trie]
- TRIE树
- TRIE树
- TRIE树
- trie 树
- Trie树
- Trie树
- Trie树
- Trie树
- trie树
- trie树
- Trie 树
- Trie树
- Trie 树
- trie 树
- Trie树
- Trie树
- Trie树
- 域名知识科普:你的域名还安稳的待在手上吗?
- win7系统卡在修复启动界面循环,登录时提示用户被锁定。
- bzoj2241: [SDOI2011]打地鼠
- java通过mybatis查询mysql服务
- Tip5 使用Int?来确保值类型也可用为null
- Trie树
- Oracle客户端安装与配置PL/SQL
- KMP算法(研究总结,字符串)
- 总线测试小结
- ArcGIS Web Adaptor配置过程常见问题(持续更新)
- OpenCV3.0 + VS2013配置二:自动化配置
- 第四方支付
- yandex.com搜索等级、限制设置
- 命名规则