HDU 6138 Fleet of the Eternal Throne (AC自动机)

解决2020R2,2022R1等版本的SCDM无法编辑草图尺寸的问题 阅读详情

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138

题意:给出n个串以及m个询问,对于每个询问(x, y),回答串x和串y的最长公共连续子串,并且这个串还是n个串中任意一个串的前缀。

思路:将这n个串建立AC自动机,每个节点保存其到根节点的距离(即前缀的长度)。首先将串x在自动机上跑一遍,将经过的节点做个标记(表示串x可以匹配到这些节点所代表的前缀),再把串y在自动机上跑一遍,若其经过的节点已经存在标记,则说明这个节点所代表的前缀既能匹配x也能匹配y,更新最大答案即可。


#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<iostream>
#include<set>
#include<map>
#include<cmath>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#define fin freopen("1006.in","r",stdin)
#define fout freopen("a.txt","w",stdout)
typedef long long LL;
using namespace std;
typedef pair<int, int> P;
const int INF = 1e8 + 10;
const int maxn = 1e5 + 10;
const int maxnode = 1e5 + 10;
const int sigma_size = 26;
string text[maxn];
char in[maxn];
int vis, ans;

struct AC
{
	int ch[maxnode][sigma_size];
	int val[maxnode];
	int f[maxnode];
	int last[maxnode];
	int flag[maxnode];
	int sz;
	void init() { sz = 1; memset(ch[0], 0, sizeof ch[0]); }
	int idx(char c) { return c - 'a'; }

	void insert(string &s)
	{
		int u = 0, n = s.size();
		for(int i = 0; i < n; i++)
		{
			int c = idx(s[i]);
			if(!ch[u][c])
			{
				memset(ch[sz], 0, sizeof ch[sz]);
				val[sz] = i+1;
				ch[u][c] = sz++;
			}
			u = ch[u][c];
		}
	}

    int getFail()
    {
    	queue<int> q;
    	f[0] = 0;
    	for(int c = 0; c < sigma_size; c++)
    	{
    		int u = ch[0][c];
    		if(u) { f[u] = 0; q.push(u); last[u] = 0; }
    	}

    	while(!q.empty())
    	{
            int r = q.front(); q.pop();
            for(int c = 0; c < sigma_size; c++)
            {
            	int u = ch[r][c];
            	if(!u) { ch[r][c] = ch[f[r]][c]; continue; }
            	q.push(u);
            	int v = f[r];
            	while(v && !ch[v][c]) v = f[v];
            	f[u] = ch[v][c];
            	last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
    	}
    }

    void print1(int j)
    {
        if(!j) return;
    	if(flag[j] != vis) flag[j] = vis;
    	print1(last[j]);
    }

     void print2(int j)
    {
        if(!j) return;
    	if(flag[j] == vis) ans = max(ans, val[j]);
    	print2(last[j]);
    }

    void Find1(string &T)
    {
    	int n = T.length();
    	int j = 0;
    	for(int i = 0; i < n; i++)
    	{
    		int c = idx(T[i]);
    		j = ch[j][c];
            if(val[j]) print1(j);
            else if(last[j]) print1(last[j]);
    	}
    }

    void Find2(string &T)
    {
        int n = T.length();
    	int j = 0;
    	for(int i = 0; i < n; i++)
    	{
    		int c = idx(T[i]);
    		j = ch[j][c];
            if(val[j]) print2(j);
            else if(last[j]) print2(last[j]);
    	}
    }

}ac;

int main()
{
	//fin; fout;
	int T;
	scanf("%d", &T);
	while(T--)
	{
		ac.init();
		int n, m, x, y;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++) { scanf("%s", in); text[i] = in; ac.insert(text[i]);}
	    ac.getFail();
	    scanf("%d", &m);
		while(m--)
		{
			ans = 0;
			++vis;
            scanf("%d%d", &x, &y);
            ac.Find1(text[x]); ac.Find2(text[y]);
            printf("%d\n", ans);
		}
	}
	return 0;
}

Fleet of the Eternal Throne

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 568    Accepted Submission(s): 264


Problem Description
> The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet's builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet's might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.
>
> — Wookieepedia

The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.

The battleships of the Eternal Fleet are placed on a 2D plane of  n  rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.


aa
bbbaaa
abbaababa
abba


If in the  x -th row and the  y -th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the  x -th row and  y -th row), at the same time the substring is a prefix of any other row (can be the  x -th or the  y -th row), the Eternal Fleet will have a risk of causing chain reaction.

Given a query ( x ,  y ), you should find the longest substring that have a risk of causing chain reaction.
 

Input
The first line of the input contains an integer  T , denoting the number of test cases. 

For each test cases, the first line contains integer  n  ( n≤105 ).

There are  n  lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed  105 .

And an integer  m  ( 1≤m≤100 ) is following, representing the number of queries. 

For each of the following  m  lines, there are two integers  x,y , denoting the query.
 

Output
You should output the answers for the queries, one integer per line.
 

Sample Input
  
1 3 aaa baaa caaa 2 2 3 1 2
 

Sample Output
  
3 3
 

Source
 

Recommend
liuyiding
 


mysql8.0安装及配置超详细教程_mysql-8.0.15-winx64 MySQL8.0配置教程, 全网最详,同时有本地mysql和小皮系统见另一篇 阅读详情

相关推荐

power bi 演唱会-佐罗

我是业务分析人员,注重使用性,因此,课程不到 30% 的内容就可以解决目前为止我碰到的所有业务问题,剩下的 70% 内容是帮助我提升,优化,升华,精益求精用的,比如第三方工具,比如代码运行速度优化,比如查询逻辑优化等等。系统性上来说,结构明晰,安排上非常有条理,层层递进;《BI 真经》,中有真经二字,略显夸大,而当我第一次证明了这点的时候,我也非常惊讶,这就是我做以下所有事情的一切动力。《BI 真经》综合考量思想性,系统性,本质性,创新性,通用性,技巧性,启发性,对比其他课程有压倒性的显著优势。

v_1090213742的博客 917

hdu6138 Fleet of the Eternal Throne AC自动机

题目:给你n个字符串,m个询问,问x,y这两个字符串的最长子串,并且这个子串也是某个字符串的前缀。 思路:AC自动机,n个串插入自动机中,在用x串跑一遍自动机,把经过的点标记下来,再用y串跑一遍,看这个点是否走过,走过的话,就有可能成为答案。 代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include #inclu

ACVevtor的博客 753

linux 内核vxlan收发包流程

1.vxlan口收包处理流程 前面几章详细介绍了vxlan接口的使用,而且已经知道vxlan是 MAC IN UDP中的封装,因此,在解封装之前,一切按照原有流程走,在此复习一下内核收发包流程(驱动层的数据处理这次不再解析,直接从__netif_receive_skb_core开始) #物理网卡处理中断,触发softirq i40e_intr └─napi_schedule_irqoff #i40e softirq把包放到per cpu backlog,触发softirq i40e_napi_p

魏言华的博客 3830

HDU 6138 Fleet of the Eternal Throne ( AC自动机)

Fleet of the Eternal Throne Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 131 Problem Description

K键盘里的青春K 855

告别轮询!手把手教你用S32K3的FlexCAN Enhanced FIFO+DMA实现高效CAN FD数据接收

本文详细介绍了如何利用S32K3芯片的FlexCAN Enhanced FIFO与DMA组合方案,实现高效CAN FD数据接收,彻底告别传统轮询和中断模式。通过深度缓冲、智能过滤和DMA联动技术,该方案在5Mbps高负载场景下将CPU占用率从42%降至3%以下,显著提升系统实时性和吞吐量。

weixin_30906185的博客 506

Python pydub实现语音停顿切分

将pcm文件批量处理成wav文件 import wave import os filepath = "data/" # 添加路径 filename = os.listdir(filepath) # 得到文件夹下的所有文件名称 #f = wave.open(filepath + filename[1], 'rb') #print(filename) for i in range(len(fi...

wangqianqianya的博客 1万+

HDU 6138 Fleet of the Eternal Throne AC自动机||后缀数组

传送门:HDU6138 题意:给出n个串和q次询问,每次询问给出x和y,表示输入的第x个和第y个串,要求找出一个最长的子串p,满足:p是x的子串,p是y的子串,且p是这n个串中某一个(可以是多个)的前缀,问p的最大长度是多少。 思路:看官方题解还很麻烦的样子,不过看dalao们的思路一下就懂了。。 将所有串插入到AC自动机里去,然后对于每个询问,先在AC自动机上查询x串,并对每一个经过的节点

WA是一笔财富 319

SAP 集成泛微 OA Open API 新建流程申请接口

《SAP ERP 与泛微 OA 的系统集成》系列文章。泛微 Open API 是基于 HTTPS 的 Restful 接口,上篇文章,我已经完成了调用业务接口之前的准备工作,这篇文章我以新建流程申请这个系统集成最重要的接口为例,讲解接口设计、泛微 API 调用的过程、遇到的问题、解决的办法。

SAP李斌,本人承诺所有原创文章均不使用任何AI生成技术,请大家放心阅读。 3568

HDU 6138 Fleet of the Eternal Throne(AC自动机)

Fleet of the Eternal ThroneHDU 6138 (AC自动机) 2017ACM暑期多校联合训练 - Team 8 1006 Fleet of the Eternal Throne题目链接Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi

南风古 350

SAP-ABAP:FI财务会计模块常用BAPI及核心用途实例说明

SAP FI模块的BAPI覆盖了从主数据管理、凭证过账到财务关账的全流程操作,是系统集成与自动化的重要工具。实际应用中需结合业务场景选择合适的BAPI,并严格遵循事务控制与校验规则。如需扩展功能,可通过BAdI或用户出口增强标准逻辑。

baidu_35680696的博客 1607

HDU 6138 2017多校第八场1006 Fleet of the Eternal Throne :AC自动机

题意:给出n( 题解:对n个串造出 自动AC机 ,这里的AC机不需要维护单词的结束点  需要维护一个每个节点到根的距离,也就是前缀的长度。然后用x跑一遍AC机,在所有匹配成功的结束节点上记上一个标记(直接记成询问次数就行了:第一次询问flag记成1,第二次记成2,这样保证每次的flag都不一样就不用清空标记了),然后这些标记点的意思就是:这个前缀是n个串中某个的前缀,且这个前缀是串x的字串。然后

辣鸡葫芦娃 1762

Python 爬虫实战:爬取豆瓣电影 Top250 完整数据

本文基于Python技术实现了豆瓣电影Top250榜单数据的合规爬取与分析。针对豆瓣电影页面的分页结构、反爬机制等特点,采用requests库获取数据,结合BeautifulSoup解析HTML节点,并通过正则表达式提取电影名称、评分、导演等核心字段。爬虫实现了随机User-Agent、请求间隔控制等反爬规避策略,严格遵守豆瓣社区使用规范。最终数据经清洗后存储为结构化Excel文件,并提供了数据可视化与增量更新方案。该方案为影视研究提供了可靠的数据获取方法,同时确保符合非商业使用、适度爬取等合规要求。

2503_91057718的博客 3464

HDU 6138 Fleet of the Eternal Throne 多校#8 AC自动机

给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 做法是把n个串建成AC自动机,前缀树中每个节点都当做结尾节点,val赋为trie树深度 然后把x串丢进自动机里,把匹配到的前缀节点染个色,再把y串丢进去,遇到同样颜色的前缀节点就更新一下答案 #include #include #include #include #include #include #i

Dream in the Night 348

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 解法1:AC自动机。做法是把n个串建成AC自动机,前缀树中每个节点都当做结尾节点,val赋为trie树深度,然后把x串丢进自动机里,把匹配到的前缀节点染个色,再把y串丢进去,遇到同样颜色...

aishangg437965的博客 130

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

HDU 6138 Fleet of the Eternal Throne [AC自动机]

HDU 6138 Fleet of the Eternal Throne [AC自动机]

ACTerminate的博客 440

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

HDU 6138 Fleet of the Eternal Throne AC自动机

题目链接:HDU 6138 Fleet of the Eternal Throne  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 862    Accepted Submission(s): 406 Pro

teddywang 376

HDU 6138-Fleet of the Eternal Throne ac自动机

Fleet of the Eternal Throne Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1035    Accepted Submission(s): 480   Problem Descr...

人生如戏 310

hdu 6138 Fleet of the Eternal Throne(AC自动机)

http://acm.hdu.edu.cn/showproblem.php?pid=6138 题目大意:给出n个字符串后,会有q个询问,每个询问会输入两个数,记为x,y。然后要求的是,x,y的 最长公共子串,而且这个最长公共子串要是n个字符串中某个字符串的前缀。输出这个串的长度。 解题思路:这题的题意很明确,首先我们要求出x,y串的公共子串,然后要在n个字符串中出现过。这样就可以用的AC自动

Merry_hj的博客 410

HDU 6138——Fleet of the Eternal Throne(AC自动机)

题意:给出n个字符串,m次查询,每次查询给两个字符串的编号,输出满足是其它字符串的前缀(一个就行)的这两个字符串的最长公共子串的长度。题意是比较简单易懂了。 题解:首先多个字符串又牵涉到匹配找公共子串的问题,就应该考虑AC自动机了,但这道题的问题不只是找最长公共子串,还要求这个子串是其它字符串的前缀。这里可以利用ac自动机fail指针的特点,回忆一下,fail指针是怎么来的,在trie树上查找单...

wookaikaiko的博客 252

hdu 6138 Fleet of the Eternal Throne (ac自动机)

Fleet of the Eternal Throne Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 521    Accepted Submission(s): 239 Problem Description > T

咸鱼有梦想 348

HDU_6138 Fleet of the Eternal Throne 【AC自动机&&思维】【静态数组】

题目链接 题目描述给定n个字符,m个查询,每个查询包括两个数字,要求从下标为这两个字符串中找到一个最长公共子串,并且这个子串为这n个字符的前缀。解题思路AC自动机模板稍作修改。写这种题目各种绝望,链表写着写着发现不行,然后就创建静态链表,接着又是一阵GG,最后还是数组写的比较好,该题解写完命已呜呼。 步入正题,我们先根据给定的n个字符创建线段树,节点之中保存这该前缀的长度。然后就是常规的建立fai

Learning Deep Learning 481

HDU 6138 Fleet of the Eternal Throne (2017多校8 - AC自动机)

题意: 给你n 个串, q个查询, 每个查询 输出串x 和 串y 的最大公共子串 且这个串 是其他某些串的前缀。 输出长度。 思路: 因为场上读错题, 以为是个极水的后缀数组。= = 其实是ac自动机 我们给n 个串建立ac自动机。 字典树中每个节点维护一个 到根的距离(即是前缀的长度) 然后给串x 跑一遍自动机, 把经过的点的标记出来, 表示 这个点即是 x 的子串, 又是某个串的

aozil_yang的博客 2154
上一篇: ZOJ 3329 One Person Game(概率dp 经典)
下一篇: POJ 2778 DNA Sequence (AC自动机+矩阵快速幂)
Interstellar_
博客等级 码龄10年 8粉丝 121原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值