HDU 2864 Repository 字典树
2015-10-31 13:17
260 查看
Repository
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3351 Accepted Submission(s): 1256
[/b]
[align=left]Problem Description[/align]
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given
a lot merchandise names in repository and some queries, and required to simulate the process.
[align=left]Input[/align]
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters
are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
[align=left]Output[/align]
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
[align=left]Sample Input[/align]
20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s
[align=left]Sample Output[/align]
0
20
11
11
2
[align=left]Source[/align]
2009
Multi-University Training Contest 4 - Host by HDU
[align=left]Recommend[/align]
gaojie | We have carefully selected several similar problems for you: 2852 2847 2845 2850 2851
给出n个串然后再询问q个串在前n个串出现的次数
因为只要出现就算那么建树的时候把串的子串也存入
因为会出现一个串中有重复出现查询串的情况所以建树的时候要标记一下
G++会超内存c++不会但是会稍微慢一点
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 100005 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) #define MAX 26 using namespace std; struct Trie{ Trie *next[MAX]; int cnt; int flag; }; Trie *root; int cnt,n,q; void creatTrie(char *str,int st,int len,int loop){ Trie *p=root,*q; FOR(i,st,len-1){ int id=str[i]-'a'; if(p->next[id]==NULL){ q=(Trie*)malloc(sizeof(Trie)); q->cnt=1; q->flag=loop; FOR(j,0,MAX-1)q->next[j]=NULL; p->next[id]=q; p=p->next[id]; } else { p=p->next[id]; if(p->flag!=loop){ p->flag=loop; p->cnt++; } } } } int find_Trie(char *str){ int len=strlen(str); Trie *p=root; FOR(i,0,len-1){ int id=str[i]-'a'; p=p->next[id]; if(p==NULL)return 0; } return p->cnt; } void deal(Trie *t){ if(t==NULL)return ; FOR(i,0,MAX-1) if(t->next[i]) deal(t->next[i]); free(t); } char str[50]; int main(){ while(rd(n)!=EOF){ root=(Trie *)malloc(sizeof(Trie)); FOR(i,0,MAX-1)root->next[i]=NULL; FOR(i,0,n-1){ rds(str); int len=strlen(str); for(int j=0;j<len;++j) creatTrie(str,j,len,i); } rd(q);FOR(i,1,q){rds(str);cout<<find_Trie(str)<<'\12';} deal(root); } return 0; } /* 20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s */
相关文章推荐
- 程序读取特定目录下的字符数
- 共用体(联合体)简介及在大小端判断的巧妙运用
- 程序读取特定目录下的字符数
- Machine Learning Algorithms Study Notes(5)—Reinforcement Learning
- 牛逼的架构 vs ‘牛逼’ 的人生
- 图学java基础篇之集合工具
- coderforce A. Case of the Zeros and Ones
- LeetCode(155) Min Stack
- cocos2dx 苹果5分辨率 适配错误
- [转]SQL注入攻防入门详解
- LeetCode(155) Min Stack
- 读《中国文学史》
- solr4.7中文分词器(ik-analyzer)配置
- 图学java基础篇之集合工具
- 64位Win7系统中配置Apache+Mysql+PHP环境
- xutils dbutils使用中遇到的问题
- level改bake
- [转]面向程序员的数据库访问性能优化法则
- 04OC之分类Category,协议Protocol,Copy,代码块block
- 简单几何(直线与线段相交) POJ 1039 Pipe