HDU 2846 Repository(字典树)
字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过.
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
char a[],b[];
struct Node
{
int id,num;
Node *sons[];
Node()
{
id = -,num = ;
for(int j = ; j < ; j++)
sons[j] = NULL;
}
};
Node *root;
Node *now,*Next;
void Build(char *str,int len,int nowid)
{
now = root;
for(int i = ; i < len; i++)
{
int m = str[i] - 'a';
if(now->sons[m] == NULL)
{
Next = new Node;
Next->id = nowid;
Next->num = ;
now->sons[m] = Next;
now = Next;
}
else
{
now = now->sons[m];
if(now->id != nowid)
{
now->id = nowid;
now->num++;
}
}
}
}
int Find(char *str,int len)
{
now = root;
for(int i = ; i < len; i++)
{
int m = str[i] - 'a';
if(now->sons[m] == NULL) return ;
now = now->sons[m];
}
return now->num;
}
void del(Node *root)
{
for(int i = ; i < ; i++)
{
if(root->sons[i] != NULL)
del(root->sons[i]);
}
delete root;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
root = new Node;
for(int i = ; i <= n; i++)
{
scanf("%s",a);
int lena = strlen(a);
for(int j = ; j < lena; j++)
Build(a+j,lena-j,i);
}
scanf("%d",&m);
for(int i = ; i <= m; i++)
{
scanf("%s",b);
int lenb = strlen(b);
printf("%d\n",Find(b,lenb));
}
del(root);
}
return ;
}
HDU 2846 Repository(字典树)的更多相关文章
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- hdu 2846 Repository (字典树)
RepositoryTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu 2846(字典树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 2846 Repository
http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
- hdu2846 Repository 字典树(好题)
把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
随机推荐
- 1.5后台修改添加TDK
manager\includes\languages\english.php //注意 是后台的语言包define('BOX_CONFIGURATION_Lin_STORE', 'TDKcss_set ...
- updating the chroot
Ubuntu may stop working after a Chrome OS update. If that's the case, update all the installed targe ...
- a标签无跳转的死链接
<a href="#" onclick="return false;">link1</a> <a href="javas ...
- JSP内置对象--request对象 (setCharacterEncoding("GBK"),getParameter(),getParameterValues(),getParameterNames(),getServletPath(),getContextPath()
使用最多,主要用来接收客户端发送而来的请求信息,他是javax.servlet.http.HttpServletRequest接口的实例化对象. public interface HttpServle ...
- Puppent 基本使用方法
简单的文件配置 master端 vim /etc/puppet/manifests/site.pp ###########################内容如下 node default{ file ...
- DotNetBar教程
DotNetBar是一组用于.NET Framework环境下的一组组件集,利用该组件集能够打造绚丽并且实用的应用程序界面,给开发人员提供极大的便利.关于DotNetBar,详情请参考其官方网站:ht ...
- c#抓取网页内容乱码的解决方案
写过爬虫的同学都知道,这是个很常见的问题了,一般处理思路是: 使用HttpWebRequest发送请求,HttpWebResponse来接收,判断HttpWebResponse中”Content-Ty ...
- 转: Ant 脚本的结构化设计
引言 Ant 脚本是由 Apache 提供的一种基于 Java 的构建工具,为 Java 开发人员所熟悉.Java 开发人员使用 Ant 脚本可以很方便地完成 Java 开发过程中常见的如拷贝文件.创 ...
- oracle中触发器的讲解
触发器在数据库里以独立的对象存储,它与存储过程和函数不同的是,存储过程与函数需要用户显示调用才执行,而触发器是由一个事件来启动运行.即触发器是当某个事件发生时自动地隐式运行.并且,触发器不能接收参数. ...
- Polycarp's problems
Polycarp's problems time limit per test 2 seconds memory limit per test 256 megabytes input standard ...