hdu 2846 字典树变形
mark: 题目有字串匹配的过程 有两点
1.为了高效的匹配子串 可以把所有的子串都预处理进去 然后字典树计数就放在最后面
2.在同一个母串处理自串的时候 会有重复的时候 比如abab 这里去重用个标记位就可以了(一开始用map 结果超时了,,, 果然还是想太简单了)
上代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#define maxn 27
using namespace std;
struct Tri
{
int num,id;
Tri*next[maxn];
Tri()
{
num=,id=;
memset(next,,sizeof(next));
}
};
void buildTri(Tri*root,string str,int ret)
{
for(int i=;i<str.size();i++)
{
int id=str[i]-'a';
if(root->next[id]==NULL) root->next[id]=new Tri();
root=root->next[id];
}
if(root->id!=ret) root->num++,root->id=ret;// 序号不一样的时候 计数 然后标记更新
}
int findTri(Tri *root,string str)
{
for(int i=;i<str.size();i++)
{
int id=str[i]-'a';
if(root->next[id]==NULL) return;
root=root->next[id];
}
return root->num;
}
int main()
{
cin.sync_with_stdio(false);
int t;
string ss;
cin>>t;
Tri *root=new Tri();//
for(int ii=;ii<=t;ii++)
{
cin>>ss;
int len=ss.size();
string zz;
map<string,int> fuck;
for(int i=;i<ss.size();i++)// 预处理过程 将所有的字串都放进去
{
for(int j=;j<=ss.size()-i;j++)
{
zz=ss.substr(i,j);
buildTri(root,zz,ii);
}
}
}
cin>>t;
while(t--)
{
cin>>ss;
cout<<findTri(root,ss)<<endl;
}
return;
}
hdu 2846 字典树变形的更多相关文章
- Repository HDU - 2846 字典树
题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...
- POJ 3764 - The xor-longest Path - [DFS+字典树变形]
题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...
- HDU 1247 Hat’s Words(字典树变形)
题目链接:pid=1247" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=1247 Pro ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- 字典树变形 A - Gaby And Addition Gym - 101466A
A - Gaby And Addition Gym - 101466A 这个题目是一个字典树的变形,还是很难想到的. 因为这题目每一位都是独立的,不会进位,这个和01字典树求最大的异或和是不是很像. ...
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
随机推荐
- JAVA基础知识|HTTP协议-两个特性
一.无连接 无连接:服务器与浏览器之间的一次连接只处理一个http请求,请求处理结束后,连接断开.下一次请求再重新建立连接. 然而随着互联网的发展,一台服务器同一时间处理的请求越来越多,如果依然采用原 ...
- sh: 1: Syntax error: Bad fd number
Start on Ubuntu 6.10,Using dash default(theDebian Almquist Shell) instead bash(the GNUBourne-Again S ...
- Flutter中用ListView嵌套GridView报错异常
flutter中的ListView组件和GridView组件都是常用的布局组件,有时候ListView中需要嵌套GridView来使用,例如下图: 这种情况就需要在ListView里面再嵌套一个Gri ...
- WIN7+Qt5.2.0连接oracle数据库的oci驱动的编译
一.前提安装 1.需要安装QT5.2.0,本介绍安装的是qt-windows-opensource-5.2.0-mingw48_opengl-x86-offline.exe: 本文安装目录:c:\Qt ...
- Understanding Models, Views, and Controllers (C#)
https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models- ...
- 分布式系统的应用程序性能监视工具,专为微服务、云本机架构和基于容器(Docker、K8s、Mesos)架构而设计。 SkyWalking
Apache SkyWalking™ | SkyWalking Teamhttp://skywalking.apache.org/zh/ Application performance monitor ...
- Coarse-to-Fine超分辨率相关
1.A Coarse-to-Fine Subpixel Registration Method to Recover Local Perspective Deformation in the Appl ...
- SQL-W3School-高级:SQL 数据类型
ylbtech-SQL-W3School-高级:SQL 数据类型 1.返回顶部 1. Microsoft Access.MySQL 以及 SQL Server 所使用的数据类型和范围. Microso ...
- PostgreSql 使用自定义序列(Sequence)向表插入数据
最近公司使用到了PostgreSql,哈哈,这个SQL之前基本上没有用过,既然公司使用到了,那就学习一下吧,记一篇小笔记: 什么是PostgreSql:https://www.postgresql.o ...
- log4net通过代码控制按分类输出
应用场景: 比如我们系统有5个任务,每个任务都是独立的流程,按照传统的方式这些流程的数据会输出到一起,这无疑给我们排查问题增加了难度,因为我们需要的是每一个任务一个独立的输出文件,比如任务A输出到lo ...