hdu 2846 Repository trie
来源:互联网 发布:独立的数据集市 编辑:程序博客网 时间:2024/05/16 06:43
#include<bits/stdc++.h>using namespace std;struct trie{ int cnt,num; int id; trie *son,*bro;};trie *rt=new trie;int tot;void settrie(char *s,int j){ trie *p=rt,*t,*t1; int n=strlen(s),i,c; for(i=0; i<n; i++) { c=s[i]-'a'; while(p->num!=-1&&p->num!=c) { t=p; p=p->bro; if(p->num>c) { p=t; break; } } //if(i==0&&c==3) printf("%d\n",++tot); if(p->num==-1) { p->cnt=1; p->num=c; t= new trie; t->id=-1; t->num=-1; t->cnt=0; p->bro=t; t= new trie; t->num=-1; t->cnt=0; t->id=-1; p->son=t; p->id=j; p=p->son; } else if(p->num==c) { if(p->id!=j) p->cnt++; p->id=j; p=p->son; } else { t= new trie; t->num=c; t->cnt=1; t->id=-1; t1= new trie; t1->id=-1; t1->num=-1; t1->cnt=0; t->son=t1; t->bro=p->bro; p->bro=t; p=t; p->id=j; p=p->son; } }}int main(){ int ans,c,n,i,j,len; char s[50]; trie *p,*t; while(~scanf("%d",&n)) { rt=new trie; rt->num=-1; rt->cnt=0; rt->id=-1; tot=0; for(i=0; i<n; i++) { scanf("%s",s); len=strlen(s); for(j=0; j<len; j++) settrie(s+j,i); } scanf("%d",&n); for(i=0; i<n; i++) { scanf("%s",s); len=strlen(s); p=rt; ans=0; for(j=0; j<len; j++) { c=s[j]-'a'; while(p->num!=-1&&p->num!=c) { t=p; p=p->bro; if(p->num>c) { p=t; break; } } if(p->num==c) { ans=p->cnt; p=p->son; } else break; } if(j!=len) ans=0; printf("%d\n",ans); } } return 0;}
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct trie{ int cnt,num; trie *next[26];};trie *rt=new trie;void settrie(char *s,int num){ trie *p=rt,*t; int n=strlen(s),i,c; for(i=0;i<n;i++) { c=s[i]-'a'; if(p->next[c]==NULL) { t=new trie; for(int j=0;j<26;j++) { t->next[j]=NULL; t->cnt=0; t->num=-1; } p->next[c]=t; } p=p->next[c]; if(p->num!=num) p->cnt++; p->num=num; }}int main(){ int ans,c,n,i,j,len; char s[50]; trie *p; while(~scanf("%d",&n)) { for(i=0;i<26;i++) { rt->next[i]=NULL; rt->cnt=0; rt->num=-1; } for(i=0;i<n;i++) { scanf("%s",s); len=strlen(s); for(j=0;j<len;j++) settrie(s+j,i); } scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s",s); len=strlen(s); p=rt; ans=0; for(j=0;j<len;j++) { c=s[j]-'a'; if(p->next[c]==NULL) break; p=p->next[c]; } if(j==len) ans=p->cnt; printf("%d\n",ans); } } return 0;}
0 0
- hdu 2846 Repository(Trie)
- hdu 2846 Repository trie
- HDU 2846 Repository (Trie)
- HDU 2846 Repository (Trie·统计子串)
- HDU-2846 Repository (Trie 字典树 入门题)
- hdu 2846 Repository
- hdu 2846 Repository
- HDU-2846-Repository
- hdu 2846 Repository
- HDU 2846 Repository
- hdu 2846 Repository
- HDU 2846 Repository
- HDU 2846 Repository
- hdu 2846 Repository
- hdu(2846)Repository
- hdu 2846 Repository
- hdu 2846 Repository
- hdu 2846Repository
- python小题目
- 【创龙TMS320C6748开发板试用】+ECAP模块初探
- 次小生成树——转载于张鹏飞博客
- unity官方demo学习之Stealth(二十)敌人视听范围
- Android系统中设置TextView的行间距(非行高)
- hdu 2846 Repository trie
- Bootstrap CSS——表单(二)
- mongodb查询中的null和存在不存在
- gcc/g++ 编译器使用简介
- 【创龙TMS320C6748开发板试用】+创龙ECAP_APWM代码分析
- ocp-132
- rspec测试出错
- 构造函数引用时的小秘密
- 中关村创业大街是贵重金属垃圾场,我捡到一只产品经理