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--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...
随机推荐
- C++中复制构造函数和赋值操作符
先看一个例子: 定义了一个类:
- keybd_event 对应表
Option Explicit Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bSc ...
- hdu_5776_sum(前缀和维护)
题目链接:hdu_5776_sum 题意: 给你一串数,问你是否有一个连续的子序列的和为m的倍数 题解: 维护一个前缀和%m的值,如果前缀和%m的值为0或者有两个前缀和%m的值相同,那么就有一个连续区 ...
- iOS 协同开发出fatal error: file 'XX-Prefix.pch' has been modified since the precompiled header was built
http://blog.sina.com.cn/s/blog_6f8ed1b90101ake6.html 解决方法 1)http://stackoverflow.com/questions/14793 ...
- 杂音 & pop 音的解决方法
杂音 & pop 音的解决方法 1. 喇叭有严重的"吱吱"破音,绝大多数的原因有可能在于V(out)电压不稳定,所以最好测一下无负载时的输出电压.同时也可以测量 VCC – ...
- mysql开启/关闭 update delete 安全模式
在使用mysql执行update的时候,如果不是用主键当where语句,会报如下错误,使用主键用于where语句中正常. 异常内容:Error Code: 1175. You are using sa ...
- make: *** No rule to make target `out
按照google的指引,一路很顺,最后make -j5的时候出现:make: *** No rule to make target `dalvik/vm/mterp/out/InterpAsm-x86 ...
- JDBC技术
JDBC是java程序操作数据库的API 一 JDBC连接数据库的过程 (1) 注册数据库驱动 Class.forName("com.mysal.jdbc.Dirver") ...
- Centos 6.4下使用VSFTPD无法正常连接与无法上传文件的问题解决
发表于 2014 年 4 月 13 日 作者 SCKA 最近利用Linux搭建服务器 搭建FTP的时候决定使用VSFTP搭建,结果却出现了无法正常连接与无法上传文件等诸多问题 经过许久的努力,终于让V ...
- Openlayers 3 图层探查功能
<body> <div id="map"></div> <script> var map=new ol.Map({ target:& ...