HDU 1251 字典树
来源:互联网 发布:linux下copy命令 编辑:程序博客网 时间:2024/05/16 10:26
题意:给你一系列单词库,接下来给你一些单词,询问有多少以该字符串为前缀的单词的数量.
#include<cstdio>#include<stdlib.h>#include<string.h>#include<string>#include<map>#include<cmath>#include<iostream>#include <queue>#include <stack>#include<algorithm>#include<set>using namespace std;#define INF 1e8#define eps 1e-8#define LL long long#define maxn 26#define mol 1000000007char s[110];struct Trie{ Trie *next[maxn]; int v;}root;void creatTrie(char *str){ int len = strlen(str); Trie *p = &root,*q; for(int i=0;i<len ;i++) { int id =str[i]-'a'; if (p->next[id]==NULL) { q=(Trie *)malloc(sizeof(Trie));q->v=1;for(int j=0;j<maxn;j++) q->next [j]=NULL;p->next [id]=q;p=q; } else { p=p->next[id]; p->v++; } }}int find (char *str){ int len = strlen(str);Trie *p=&root;for(int i=0;i<len;i++){ int id = str[i]-'a';if(p->next[id])p = p->next[id];else return 0;}return p->v ;}int main(){int i,n=0;char ch[20];for(i=0;i<maxn;i++)root.next [i]=NULL;while(gets(s)&&s[0]!='\0') creatTrie(s);while(gets(ch)) printf("%d\n",find(ch));return 0;}/*bananabandbeeabsoluteacmbabbandabc*/
0 0
- hdu 1251(字典树)
- hdu 1251 字典树
- HDU 1251 字典树
- hdu 1251 字典树
- hdu 1251 字典树
- HDU-1251,字典树
- HDU 1251 字典树
- 1251 hdu 字典树
- HDU 1251(字典树)
- HDU 1251 字典树
- HDU 1251(字典树)
- HDU 1251 字典树
- hdu 1251 字典树
- HDU 1251 字典树
- 字典树 hdu 1251
- hdu 1251 字典树
- HDU 1251 字典树
- hdu 1251 字典树
- 在lua中使用cocostudio创建的滚动条
- Java中的main()方法
- JDK版本不对引起的错误
- (转载)IT未来发展五大趋势
- Nginx+tomcat+Java
- HDU 1251 字典树
- 编程语言类型
- 第一课二叉树
- 个性化推荐数据产品与应用
- c++ 虚表
- Install Android SDK, Eclipse, and Emulator (AVDs)
- oracle海量数据去重方法
- 执行netca报错 core-dump
- 【android】二维码、条形码扫描——使用Google ZXing