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 ...
随机推荐
- Flutter移动电商实战 --(31)列表页_列表切换交互制作
点击左侧的大类右边的小类也跟着变化 新建provide 要改变哪里就建哪里的provide,我们现在要改变的是右边的商品列表的数组. category_goods_list.dart 这样我们的pro ...
- Java同步数据结构之CopyOnWriteArrayList/CopyOnWriteArraySet
前言 前面介绍完了队列(包括双端队列),今天探讨以下Java并发包中一个List的并发数据结构实现CopyOnWriteArrayList,顾名思义CopyOnWriteArrayList也是一种基于 ...
- LC 983. Minimum Cost For Tickets
In a country popular for train travel, you have planned some train travelling one year in advance. ...
- 19.网络插件calico
19.网络插件calico 官网: https://docs.projectcalico.org/v3.8/introduction/ calico默认工作在192.168.0.0/16 的网络 ca ...
- TortoiseSVN commit 停止工作
TortoiseSVN commit 便停止工作,详细原因是igc64.dll故障,该动态链接库与Intel HD Graphics Driver有关(即显卡驱动),由于重装系统后,进行了显卡驱动的更 ...
- oracle增删改查基本语法(DDL/DML)整理
DDL:Data Definition Language(数据库定义语言)DML:Data Manipulation Language(数据库操作语言) 一.表结构操作(create table.al ...
- [maven]maven插件 tomcat7-maven-plugin 的使用
使用 tomcat7-maven-plugin,可以将tomcat内嵌到web项目中,直接运行webapp项目. 第一步.pom.xml的配置: <build> <plugins&g ...
- smart_pointer example
#pragma oncetemplate<typename T>class smart_pointer{private: T* m_pRawPointer;public: smart_po ...
- JAVA 基础编程练习题40 【程序 40 字符串排序】
40 [程序 40 字符串排序] 题目:字符串排序. package cskaoyan; public class cskaoyan40 { @org.junit.Test public void a ...
- 使用linux flock文件锁实现任务锁定避免计划任务程序冲突
格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --shared: 获得一个共享锁 -x, --exclusi ...