hdu2846(动态字典树||静态字典树 模板)Repository
2017-07-18 16:51
197 查看
Repository
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 5464 Accepted Submission(s): 1832
Problem Description
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.
Input
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.
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
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
Sample Output
0
20
11
11
2
询问一个字符串在上面几个字符串出现过
//静态字典树
//字符串的复制要注意多复制一位
//题目给出的数据比较大,那样就超空间了,然而事实数据没那么大
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int mn=500010;
int tot,n;
int str[mn][26];
int vis[mn];//标志该条路径的字符
int num[mn];//记录到达该结点的个数
void creat(char* s,int k)
{
int len=strlen(s),p=0;
for(int i=0;i<len;++i)
{
int id=s[i]-'a';
if(!str[p][id]) str[p][id]=tot++;
p=str[p][id];
if(vis[p]!=k)
{
num[p]++;
vis[p]=k;
}
}
}
int find(char* s)
{
int p=0,len=strlen(s);
for(int i=0;i<len;++i)
{
int id=s[i]-'a';
if(!str[p][id]) return 0;
p=str[p][id];
}
return num[p];
}
int main()
{
char s[21],st[21];
int l=sizeof(char);
while(~scanf("%d",&n))
{
memset(str,0,sizeof(str));
memset(num,0,sizeof(num));
tot=1;
while(n--)
{
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;++i)
{
//memcpy(st,s+i,(len-i+1)*l);
memcpy(st,s+i,len-i+1);
creat(st,n+1);
}
}
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
printf("%d\n",find(s));
}
}
return 0;
}
//动态字典树 //字符串的复制要注意多复制一位 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> using namespace std; struct Tire{ int num,vis; Tire *next[26]; Tire() { num=0,vis=0; //用malloc构建一个结点,这个num的初始化居然会出错 for(int i=0;i<26;++i) next[i]=NULL; } }; Tire *root=new Tire; void creat(char* s,int k) { int len=strlen(s); Tire *p=root; for(int i=0;i<len;++i) { int id=s[i]-'a'; if(p->next[id]==NULL) p->next[id]=new Tire; p=p->next[id]; if(p->vis!=k) { p->num++;//两个运算符同一优先级,从左到右 p->vis=k; } } } int find(char* s) { int len=strlen(s); Tire *p=root; for(int i=0;i<len;++i) { int id=s[i]-'a'; if(p->next[id]==NULL) return 0; p=p->next[id]; } return p->num; } void Delete(Tire* p) { //释放字典树的空间 for(int i=0;i<26;i++) if(p->next[i]) Delete(p->next[i]); delete(p); } int main() { char s[21],st[21]; int l=sizeof(char),n; while(~scanf("%d",&n)) { root=new Tire; while(n--) { scanf("%s",s); int len=strlen(s); for(int i=0;i<len;++i) { //memcpy(st,s+i,(len-i+1)*l); memcpy(st,s+i,len-i+1); creat(st,n+1); } } scanf("%d",&n); while(n--) { scanf("%s",s); printf("%d\n",find(s)); } Delete(root); } return 0; }
相关文章推荐
- HDU:1251-统计难题(字典树模板,动态建树,静态建树)
- 利用C++模板,代替虚函数实现类的静态多态性及动态继承
- hdu2846 Repository (字典树)
- Phone List (POJ_3630) 静态字典树 + 模板题
- hdu2846 Repository -字典树
- 字典树 静态建树与动态建树
- 使用freemarker实现静态模板维护及动态内容展示案例
- hdu2846 Repository 字典树(好题)
- 利用C++模板,代替虚函数实现类的静态多态性及动态继承
- 【C/C++学院】0823-静态联合编译与动态联合编译/父类指针子类指针释放/虚函数/纯虚函数概念以及虚析构函数/抽象类与纯虚函数以及应用/虚函数原理/虚函数分层以及异质链表/类模板的概念以及应用
- hdu2846 Repository(字典树)
- HDU1251(静态维护字典树模板题)
- Eclipse自定义类注释模板设置--伪静态首页的拦截☞动态jsp跳转
- 【Trie】Trie字典树模板 静态指针池、数组写法
- Trie简要模板(动态实现和静态实现)
- poj 3630 / hdu 1671 Phone List 【Trie字典树 动态创建&静态创建】
- ZendFramework2学习笔记 静态配置layout模板和动态设置layout模板
- 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板
- JAVA根据模板将动态内容生成静态网页的代码
- 利用C++模板,代替虚函数实现类的静态多态性及动态继承