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

HDU 6138 Fleet of the Eternal Throne ( AC自动机)FleetoftheEternalThroneTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):291    AcceptedSubmission(s):131ProblemDescription

大家好,又见面了,我是你们的朋友全栈君。

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
> 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  (

n105 ).

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  (

1m100 ) 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   |   We have carefully selected several similar problems for you:  
6143 
6142 
6141 
6140 
6139 

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


原来模板太不好变化了。。有改了下

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1e6 + 7;
const int maxnode = 50*10010;
int n;
char s1[60], s2[maxn];
int pos[maxn];
struct node
{
    int ch[maxnode][26], cnt[maxnode], fail[maxnode], last[maxnode], id, road[maxnode]; // 路径压缩优化, 针对模式串出线的种类
    int dep[maxnode], flag[maxnode];
    void init()
    {
        memset(ch[0], 0, sizeof(ch[0]));
        memset(dep, 0, sizeof(dep));
        id = 1;
    }
    void Insert(char *s)
    {
        int rt = 0;
        int len = strlen(s);
        dep[rt] = 0;
        for(int i = 0; i < len; i++)
        {
            if(!ch[rt][s[i]-'a'])
            {
                memset(ch[id], 0, sizeof(ch[id]));
                flag[id] = 0;
                ch[rt][s[i]-'a'] = id++;
            }
            dep[ch[rt][s[i]-'a']] = dep[rt] + 1;
            rt = ch[rt][s[i]-'a'];
        }
    }
    void get_fail()
    {
        queue<int> q;
        fail[0] = 0;
        int rt = 0;
        for(int i = 0; i < 26; i++)
        {
            rt = ch[0][i];
            if(rt)
            {
                q.push(rt);
                fail[rt] = 0;
            }
        }
        while(!q.empty())
        {
            int r = q.front();
            q.pop();
            for(int i = 0; i < 26; i++)
            {
                rt = ch[r][i];
                if(!rt)
                {
                    ch[r][i] = ch[fail[r]][i];
                    continue;
                }
                q.push(rt);
                fail[ch[r][i]] = ch[fail[r]][i];
            }
        }
    }
    void Match(char *s, int f)
    {
        int rt = 0;
        int len = strlen(s);
        for(int i = 0; i < len; i++)
        {
            int temp = rt = ch[rt][s[i]-'a'];
            while(temp)
            {
                flag[temp] = f;
                temp = fail[temp];
            }
        }
    }
    int Search(char *s, int f)
    {
        int rt = 0, res = 0;
        int len = strlen(s);
        for(int i = 0; i < len; i++)
        {
            int temp = rt = ch[rt][s[i]-'a'];
            while(temp)
            {
                if(flag[temp] == f) res = max(res, dep[temp]);
                temp = fail[temp];
            }
        }
        return res;
    }
}ac_auto;
char s[maxn];
int main()
{
    int t, q;
    cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        int d = 0;
        ac_auto.init();
        for(int i = 0; i < n; i++)
        {
            pos[i] = d;
            scanf(" %s", s+d);
            ac_auto.Insert(s+d);
            int len = strlen(s+d);
            d += len + 1;
        }
        ac_auto.get_fail();
        scanf("%d", &q);
        int ca = 1;
        while(q--)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            x--; y--;
            ac_auto.Match(s+pos[x],ca);
            int ans = ac_auto.Search(s+pos[y], ca++);
            printf("%d\n", ans);
        }
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/138514.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 利用ffmpeg制作gif图[通俗易懂]

    利用ffmpeg制作gif图[通俗易懂]由于老师安排的作业有需求,这里就记录一下利用ffmpeg制作gif图1.确保ffmpeg安装运行会显示版本号:ffmpeg-version没有安装的话先安装:sudoapt-repositoryppa:kirillshkrogalev/ffmpeg-nextsudoapt-getupdatesudoapt-getinstallffmpegffmpeg-ve…

    2025年6月26日
    2
  • ssm管理系统课题_p2实验室

    ssm管理系统课题_p2实验室开发目的方便高效地实验室设备统一管理,除了实现基本的增删改查,还提供借用、归还、购买和问题反馈功能,可实现对实验室设备的基本业务的处理本项目由本人负责开发完成,项目能保证正常运行,当然其中不免也会有缺漏或不完善的地方解决方案1.后端Java框架使用spring+springmvc+mybatisspring功能是实现参数参数注入,请求分发处理,对数据库操作进行事务控制,其中mybatis使用注解查询,整体上大部分使用xml配置,少部分使用注解2.前端使用HTML+javascript+css+j

    2022年10月13日
    2
  • flask 的 jsonify 自动排序问题

    flask 的 jsonify 自动排序问题背景·Python在写接口的时候有时需要返回json格式的数据给客户端·最简单的方式就是用flask的jsonify,能直接将字典格式化为json的形式进行传输例如fromflaskimportjsonify………defreturn_success(data):”””返回成功信息”””returnjsonify(data)通过以上jsonify的方式大部分需求是能够搞定的问题引发但是有时候我们要传递的json格式可能

    2022年5月23日
    36
  • 怎么使用matlab画图_matlab画图plot

    怎么使用matlab画图_matlab画图plot注意点matlab中分号;是拿来分隔列并抑制输出显示的。matlab中每个变量都是数组/矩阵,即使是x=1这样的语句也代表创建了一个1*1的矩阵,变量使用前必须有值。通过whos显示当前的变量使用情况。长任务可以通过使用省略号…扩展到另一行。formatlong命令显示十进制后的16位数字。b白色、r红色、k黑色基本语法示例循环、选择、函数定义function…

    2022年9月20日
    4
  • 软件漏洞分析简述

    软件漏洞分析简述软件漏洞1.1漏洞的定义漏洞,也叫脆弱性(英语:Vulnerability),是指计算机系统安全方面的缺陷,使得系统或其应用数据的保密性、完整性、可用性、访问控制等面临威胁。漏洞在各时间阶段的名称根据是否公开分为:未公开漏洞、已公开漏洞根据漏洞是否发现分为:未知漏洞、已知漏洞根据补丁和利用价值是否发布分为:0day漏洞、1day漏洞、历史漏洞图1漏洞在各时间阶段的名称漏洞的特…

    2022年5月20日
    60
  • c语言指针用法详解,通俗易懂超详细!

    c语言指针用法详解,通俗易懂超详细!文章转自 无际单片机大家好 我是无际 今天给大家来讲解一下指针 我会由浅到深 最后结合实际应用讲解 让大家学会指针的同时 知道大佬们都用指针来干嘛 长文预警 全文大约 5200 多字 学指针看

    2025年11月4日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号