hdu 2846 字典树
来源:互联网 发布:淘宝网儿童棉服 编辑:程序博客网 时间:2024/04/30 08:18
给出N个字符串,给出M个询问。
问该串是多少个串的子串。
字典树。。
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 2005#define inf 1<<28#define LL(x) (x<<1)#define RR(x) (x<<1|1)#define ll long longusing namespace std;struct Trie{ int count;//记录经过这个分支的个数 int id; struct Trie *next[26]; Trie() { id=0; count=0; memset(next,0,sizeof(next)); }};Trie *root=NULL;void build_Trie(char *s,int id)//建树{ Trie *p=root; Trie *temp=NULL; int l=strlen(s); for(int i=0; i<l; i++) { int index=s[i]-'a'; if(p->next[index]==NULL) { temp=new Trie; p->next[index]=temp; } p=p->next[index]; if(p->id!=id) { p->count++; p->id=id; } }}int Trie_search(char *s){ Trie *p=root; int l=strlen(s); for(int i=0; i<l; i++) { int index=s[i]-'a'; if(p->next[index]==NULL) return 0; p=p->next[index]; } //cout<<endl; return p->count;}int main(){ char a[1002]; int i=0; root =new Trie; int n,m; cin>>n; for(int i=1; i<=n; i++) { cin>>a; int l=strlen(a); for(int j=0; j<l; j++)//对每个子串建一个分支 build_Trie(&a[j],i); } cin>>m; while(m--) { cin>>a; cout<<Trie_search(a)<<endl; } return 0;}
- HDU 2846 字典树
- HDU 2846 字典树
- hdu 2846 字典树
- hdu 2846 字典树
- hdu 2846-字典树
- 【字典树】hdu 2846 Repository
- hdu 2846 经典字典树
- hdu 2846 Repository (字典树)
- HDU--2846--Repository--字典树
- hdu 2846 Repository (字典树)
- hdu 2846 Repository 字典树
- HDU 2846 Repository (字典树)
- HDU 2846 字典树变形
- hdu 2846 简单字典树
- hdu 2846霸气的字典树
- HDU 2846 Repository(字典树,计数)
- HDU-2846Repository(字典树)
- HDU 2846 Repository(字典树变形)
- POJ1423,Big Number,同hdu1018
- POJ1694,An Old Stone Game,读懂题目+dfs
- 看谭浩强C++的感想
- 谢谢你,勾引我老公!这文章看得心酸
- 从非Exchange 系统迁移邮件数据到Exchange Server!
- hdu 2846 字典树
- 性能改善的一种模式
- 诺基亚中国市场严重衰退销量剧减64%
- EffectiveC++ Item12
- 繁体字非主流伤感日志_親愛的,為什麼
- 利用无序数组实现优先队列并排序数组——ADT实现
- hdu 1869 六度分离
- c语言的变量
- 邻居发现(Neighbor Discovery)协议