字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个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(字典树)的更多相关文章

  1. HDU 2846 Repository (字典树 后缀建树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  2. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  3. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. hdu 2846 Repository

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others)     ...

  7. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  8. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

  9. hdu2846 Repository 字典树(好题)

    把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...

随机推荐

  1. js算数方法

    原文:http://www.w3school.com.cn/js/js_obj_math.asp 除了可被 Math 对象访问的算数值以外,还有几个函数(方法)可以使用. 函数(方法)实例: 下面的例 ...

  2. $.getjson方法配合在url上传递jsoncallback=?参数,实现跨域获取指定网站某商品访问量

    across.php文件在域名www.cms.com程序中 <html><body><div id="pv">99</div>< ...

  3. 【Machine Learning in Action --2】K-最近邻分类

    1.K-近邻算法(KNN)概述 K-近邻算法采用测量不同特征值之间的距离方法进行分类. 工作原理:存在一个样本数据集合(也称作训练样本集),并且样本集中每个数据都存在标签(即我们知道样本集中每一数据与 ...

  4. CentOS7 PostgreSQL 安装

    PostgreSQL安装 安装使用yum安装 (找源 http://yum.postgresql.org/) yum install https://download.postgresql.org/p ...

  5. postgres-xl 集体搭建(1)

    安装并编辑脚本 cd /opt/curl -O http://files.postgres-xl.org/postgres-xl95r1beta1.tar.gztar -zxvf postgres-x ...

  6. linux 进程监控和自动重启的简单实现(转)

    目的:linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能. 实现原理:由定时任务crontab调用脚本,脚本用ps检查进程是否存在,如果不存在则重 ...

  7. asp.net如何删除文件夹及文件内容操作

    static void DeleteDirectory(string dir) { && Directory.GetFiles(dir).Length == ) { Directory ...

  8. 实现jsp页面显示用户登录信息,利用session保存。

    这是后台代码 这是jsp代码,上面是声明,下面是获得值.

  9. 8.4 sikuli 集成进eclipse 报错:Unsupported major.minor version 51.0

    8.3中的问题Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform  解决之后,执行还是会有报错:Unsupported maj ...

  10. web项目的集成测试:模拟点击

    利用模拟点击的方式进行集成测试,我们准备用google,Firefox和ie三种浏览器测试. 其中除了Firefox是模拟点击的鼻祖不需要插件外,谷歌和ie需要另外下载插件 chromedriver. ...