字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个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. Html5中的本地存储

    Web Storage web storage页面存储是html5为数据存储在客户端提供的一项重要功能,由于web storage API能够区分会话数据与长期数据.因此,相应API也分为两种: se ...

  2. Shiro 的FilterChain

    /** * Shiro的FilterChain * @see ===================================================================== ...

  3. knockout.js

    最近在使用knockout这个JS的MVVM模型,真的很不错,每次去查英文的文档,的确很累的,抽空的时候就把看到的文档按自己的理解翻译一下.当然我不是逐字的翻译. knockout的官方说明:http ...

  4. Eclipse报错 due to restriction on required library C:/Java/jdk1.7.51/jre/lib/rt.jar 解决方案

    Eclipse报错 due to restriction on required library C:/Java/jdk1.6.0_10/jre/lib/rt.jar 解决方案 Eclipse 编译时 ...

  5. s7-300 第二讲

  6. 关于Winform中的用户代理

    问题描述: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 解释: 1. 应用程序版本“Mozilla ...

  7. vs2013安装visual assist和viemu之后提示功能等无效解决

    1.vs2013安装了上面两个软件之后会发生va功能无效,经过一番谷歌百度后找到了解决方案 1.打开注册表 2.直接搜索TrackCaretVisibility这个键值,找到后把他的值修改成00 此篇 ...

  8. JDBC 事务隔离级别

    JDBC 事务隔离级别     先解释一下:a:脏读取:一个事务读取了另外一个并行事务未提交的数据b:不可重复读取:一个事务再次读取之前的数据时得到的数据不一致,被另外一个事务修改c:虚读:一个事务重 ...

  9. UITextField和一个UILabel绑定 浅析

    转载自:http://fengdeng.github.io/blog/2016/01/22/rxswift-dao-di-%5B%3F%5D-ge-uitextfieldshi-ru-he-he-%5 ...

  10. 根据key存不存在查询json

    select *  from  table where value->'key' != 'null';