URAL 1297 最长回文子串(后缀数组)

华为铁三角(AR、SR、FR)的考核指标设计 以和为核心,通过和实现协同。考核维度分为和,并随市场阶段动态调整权重。 阅读详情

1297. Palindrome

Time limit: 1.0 second
Memory limit: 64 MB
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).
Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.
So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
Your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. As NPRx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.

Input

The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.

Output

The longest substring with mentioned property. If there are several such strings you should output the first of them.

Sample

input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
output
ArozaupalanalapuazorA


/*
URAL 1297 最长回文子串(后缀数组)

算法合集之《后缀数组——处理字符串的有力工具》:
穷举每一位,然后计算以这个字符为中心的最长回文子串。注意这里要分两
种情况,一是回文子串的长度为奇数,二是长度为偶数。两种情况都可以转化为
求一个后缀和一个反过来写的后缀的最长公共前缀。具体的做法是:将整个字符
串反过来写在原字符串后面,中间用一个特殊的字符隔开。这样就把问题变为了
求这个新的字符串的某两个后缀的最长公共前缀。

所以我们只需先对初始的字符串进行一下处理,然后分别进行奇偶判断得到
最长回文子串的位置和长度

hhh-2016-03-13 15:41:30
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define lson (i<<1)
#define rson ((i<<1)|1)
const int maxn = 5005;

int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b] &&r[l+a] == r[l+b];
}

void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
    n++;
    int p,*x=t1,*y=t2;
    for(int i = 0; i < m; i++) c[i] = 0;
    for(int i = 0; i < n; i++) c[x[i] = str[i]]++;
    for(int i = 1; i < m; i++) c[i] += c[i-1];
    for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
    for(int j = 1; j <= n; j <<= 1)
    {
        p = 0;
        for(int i = n-j; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
        for(int i = 0; i < m; i++) c[i] = 0;
        for(int i = 0; i < n; i++) c[x[y[i]]]++ ;
        for(int i = 1; i < m; i++) c[i] += c[i-1];
        for(int i = n-1; i >= 0; i--)  sa[--c[x[y[i]]]] = y[i];

        swap(x,y);
        p = 1;
        x[sa[0]] = 0;
        for(int i = 1; i < n; i++)
            x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++;
        if(p >= n) break;
        m = p;
    }
    int k = 0;
    n--;
    for(int i = 0; i <= n; i++)
        Rank[sa[i]] = i;
    for(int i = 0; i < n; i++)
    {
        if(k) k--;
        int j = sa[Rank[i]-1];
        while(str[i+k] == str[j+k]) k++;
        height[Rank[i]] = k;
    }
}


int mm[maxn];
int dp[20][maxn];
int Rank[maxn],height[maxn];
int sa[maxn],str[maxn];
char ts[maxn];

void ini_RMQ(int n)
{
    mm[0] = -1;
    for(int i = 1;i <= n;i++)
        mm[i] = (((i & (i-1)) == 0) ? mm[i-1]+1:mm[i-1]);

    for(int i =1;i <= n;i++)
        dp[0][i] = height[i];
    for(int i = 1;i <= mm[n];i++)
    {
        for(int j = 1;j+(1<<i)-1 <= n;j++)
        {
            int a = dp[i-1][j];
            int b = dp[i-1][j+(1<<(i-1))];
            dp[i][j] = min(a,b);
        }
    }
}

int askRMQ(int a,int b)
{
    int t = mm[b-a+1];
    b -= (1<<t)-1;
    return min(dp[t][a],dp[t][b]);
}


int fin(int a,int b)
{
    a = Rank[a],b = Rank[b];
    if(a > b) swap(a,b);
    return askRMQ(a+1,b);
}

int main()
{
    while(scanf("%s",ts) != EOF)
    {
        int len = strlen(ts);
        for(int i = 0;i < len;i++)
            str[i] = ts[i];
        str[len] = 1;
        for(int i = 0;i < len;i++)
            str[i+len+1] = ts[len-i-1];
        str[len*2+1] = 0;

        get_sa(str,sa,Rank,height,2*len+1,128);
        ini_RMQ(2*len+1);

        int ans = 0,pos;
        int tp;
        for(int i = 0;i < len;i++)
        {
            tp = fin(i,len*2+1-i);
            if(tp*2 > ans)
            {
                ans = tp*2;
                pos = i-tp;
            }
            tp = fin(i,len*2-i);
            if(tp*2-1 > ans)
            {
                ans = tp*2-1;
                pos = i-tp+1;
            }
        }
        ts[pos+ans] = 0;
        printf("%s\n",ts+pos);
    }
    return 0;
}


从零开始:用AutoLisp为AutoCAD定制专属工具栏(2023最新版) 本文详细介绍了如何利用AutoLisp语言为AutoCAD定制专属工具栏,以提升绘图效率。内容涵盖从环境配置、AutoLisp基础语法到实战开发的全过程,指导用户编写自动化脚本、封装功能函数,并最终通过CUI编辑器创建和部署个性化工具栏按钮,是2023年CAD用户实现工作流自动化与个性化的实用指南。 阅读详情

相关推荐

maxwell与steady state thermal 通过workbench耦合仿真

文章目录maxwellsteady state thermal maxwell 1加入maxwell并打开 2导入模型 3选择求解器类型:涡流求解器eddy current 4修改模型名字不能带_等特殊符号 5为模型赋材料 6设立3D边界,各边电流进出方向为0其余方向300 7设置激励条件,可设置电流大小,相位,方向 8设置集肤深度,选取中间铜导体的体然后设置,如何设置看帮助文档 9求解设置 迭代步数 收敛精度 交流电频率 10验证条件满足计算 11求解 发现无法求解,删除ai

roymustang的博客 9199

URAL 1297 Palindrome(最长回文子串:后缀数组)

后缀数组的讲解:https://wenku.baidu.com/view/ed1be61e10a6f524ccbf85fd.html 题意: 给你一个字符串,要你求出这个串中的最长回文字串,如果存在多个,则输出第一次出现的那个. 分析: 来自:https://blog.youkuaiyun.com/u013480600/article/details/23946429 首...

pxlsdz的博客 429

智能车‘大脑’的诞生:揭秘电磁循迹代码背后的控制论与系统思维

本文深入探讨智能车电磁循迹系统的核心架构与控制原理,揭示了其作为精密闭环系统如何通过模块化设计、信号处理、PID控制算法及电机驱动实现自主导航。重点分析了电磁信号采集与处理流程框架,为开发者提供从理论到实践的全面指南。

arduino9maker的博客 531

最长回文子串(后缀数组)

这个也可以利用manacher算法来做 奇数长度的回文串是 从i开始的后缀 与 反转后字符串中从原来的i位子开始的后缀 的公共前缀  偶数长度类似  所以和最长公共子串类似的思路, 将原串S与反转后的S中间加入一个不会出现的字符拼接在一起, 对得到的字符串S’求高度数组  以原来S[i]为中心的奇数长度的回文串长度 = 后缀S’[i…]和后缀 S’[2n-i…]的公共前缀  以原来S[i-1]和...

肘子的博客 1347

开源滑翔伞升降速度表:基于STM32与传感器融合的DIY飞行仪表

传感器融合是现代嵌入式系统与物联网设备中的核心技术,它通过整合多个传感器的数据,利用滤波与估计算法,获得比单一传感器更精确、更可靠的环境感知能力。其技术价值在于提升系统的鲁棒性、精度和上下文理解能力,广泛应用于无人机导航、可穿戴设备、工业监测及智能驾驶等领域。本文聚焦于一个具体的工程实践案例——开源多功能滑翔伞升降速度表(Open-Vario),该项目巧妙地运用了**卡尔曼滤波器**进行气压与GNSS数据的融合,以精确计算升降率,并基于**FreeRTOS**实时操作系统构建了一个稳定、可扩展的嵌入式平台,

weixin_33657398的博客 176

Gemma 4 12B QAT+MTP小显存部署实战指南

大语言模型本地部署的核心瓶颈在于显存与推理效率的平衡。量化感知训练(QAT)通过在训练阶段模拟低精度计算,显著提升模型对INT4量化的鲁棒性;多令牌预测(MTP)则通过并行解码优化KV Cache复用,降低GPU kernel调用频次。二者协同可在8GB显存设备上稳定运行12B级模型,兼顾精度、速度与上下文长度。该方案适用于开发者本地AI助手、技术文档生成、SQL翻译等轻量但高交互需求场景,是当前Windows环境中小显存高效落地Gemma系列模型的工程最优解。

weixin_30934229的博客 376

Palindrome

The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security s

态度决定高度 533

Ollama-OCR实战:5分钟搞定PDF文档转Markdown(附qwen2.5vl模型配置)

本文详细介绍了如何利用Ollama本地部署多模态大模型qwen2.5vl,结合OCR技术,实现PDF文档到Markdown的端到端智能转换。通过解析传统OCR的局限与AI方案的优势,并提供从环境搭建、提示词工程到处理复杂表格的完整实战指南,帮助用户高效构建自动化文档处理流水线,彻底重塑文档处理工作流。

yog99的博客 604

URAL - 1297 最长回文子串

 穷举每一位,然后计算以这个字符为中心的最长回文子串。注意这里要分两种情况,一是回文子串的长度为奇数,二是长度为偶数。两种情况都可以转化为求一个后缀和一个反过来写的后缀的最长公共前缀。具体的做法是:将整个字符串反过来写在原字符串后面,中间用一个特殊的字符隔开。这样就把问题变为了求这个新的字符串的某两个后缀的最长公共前缀。   #include &lt;iostream&gt; #incl...

兜率工的博客 307

VSCode配置C/C++开发环境:从零安装MinGW-w64并运行第一个C程序

在Windows系统上搭建C语言开发环境时,很多初学者混淆编辑器、编译器与调试器的分工,导致配置过程屡屡受挫。理解VSCode作为轻量级编辑器的定位,以及MinGW-w64提供的gcc编译器在源码编译中的核心作用,是顺利完成环境配置的关键。环境变量的正确设置直接影响gcc命令能否全局调用,而C/C++插件则赋予编辑器语法高亮与智能提示能力。通过安装VSCode、配置MinGW-w64、安装插件、汉化界面并运行一个最简单的hello.c程序,就能系统掌握这套开源工具链的完整工作流。无论是用于课程学习、算法练习

weixin_42531923的博客 531

【Linux0.11代码分析】之 系列文章链接汇总(全)

【Linux0.11代码分析】00 之 系列文章链接汇总(全)

|~~~热爱生活、努力学习的小伙汁~~~| 1449

最长回文(后缀数组||Manacher算法)

转载请注明出处,谢谢http://blog.youkuaiyun.com/acm_cxlove/article/details/7854526       by---cxlove 最长回文是一个经典的例子。 首先是URAL 1297。范围只有1000,可以用后缀数组解决 在原串后面加一个特殊符号之后,将原串反向之后加在后面,然后比较height的最大值。 其中需要判断两个sa,一个在前面一个在后面

窝不是爱酱,喵~~~~ 4215

大数据合规工程师必备的7项核心技能

当我们谈论“大数据”时,往往先想到它的价值——驱动业务增长、优化用户体验、赋能智能决策。大数据的价值越大,其合规风险的“重量”就越重。2023年,全球有超过120个国家出台了数据保护法规,中国《个人信息保护法》《数据安全法》的实施进入深水区,欧盟GDPR的罚款金额屡创新高(最高达全球营收的4%)。企业面临的合规压力,早已从“被动应对监管”升级为“主动构建合规能力”。在这样的背景下,大数据合规工程师。

SuperAGI2025 1259

ural 1297 后缀数组 最长回文子串

https://vjudge.net/problem/URAL-1297 题意: 给出一个字符串求最长回文子串 代码: //论文题,把字符串反过来复制一遍到后边,中间用一个没出现的字符隔开,然后就是枚举当以i位置为中间位时的最长回文串 //是多大,就是求这中间的heigh数组的最小值。用rmq预处理。枚举位置时分回文串长度是奇数还是偶数。 #include<iost...

anuancong1219的博客 193

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

【后缀数组】【最长回文子串】Palindrome Ural_1297

Ural_1297. Palindrome Time Limit: 1.0 second Memory Limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots

Jiangzh_MZOI 709

jre9下载 jdk9

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

ural 1297(求最长回文子串,后缀数组)

1297. PalindromeTime Limit: 1.0 secondMemory Limit: 16 MBThe “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security

-- bird -- 2983

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【后缀数组|最长回文子串】URAL-1297 Palindrome

输入一个字符串,求出它的子串中最长的回文串。

JSure的代码库 1271

ural 1297 Palindrome求最长连续回文子串(后缀数组求法)

ural 1297 Palindrome 题意:很简单,求最长连续回文子串。 解法:这题数据范围非常小,长度只有2000,用dp,n^2可以做,但如果长度变成100000了呢?后缀数组就可以发挥威力了。将原串翻转后接到原串后面,构成一个新的串,枚举前n个字符,设该字符为i,以它为中点的最长回文子串的长度就是。。自己去算算吧,很简单的,注意考虑奇偶。 #include #include #in

No__stop 1139

URAL 1297 Palindrome【后缀数组】求最长回文子串

Description The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. R

Joeferyの博客 964

ural1297 后缀数组扩展,最长回文子串

后缀数组扩展 #include<cstdio> #include<algorithm> #include<iostream> #include<cmath> #include<cstdlib> #include<cstring> #include<string> #include<map> #...

甦萌的博客 273

URAL - 1297 Palindrome —— 后缀数组 最长回文子串

题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It stat...

alince20008的博客 180

ural 1297 最长回文子串 后缀数组

http://acm.timus.ru/problem.aspx?space=1&num=1297 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymo

ACM,再见! 993

Palindrome URAL - 1297 后缀数组求最长回文子串

The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security ...

Top_Spirit的博客 338

【URAL】1297 Palindrome 【后缀数组+RMQ——求最长回文子串】

传送门:【URAL】1297 Palindrome 题目分析:将s串倒过来接到原串的后面,中间用'$'隔开,然后我们构造后缀数组和height数组,接着我们RMQ预处理,接下来枚举串的每个点作为回文串的中心,分别以该点为奇回文串中心以及偶回文串中心求LCP,此时向左和向右的串的LCP即以这个点作为中心的回文串的扩展半径,求半径的话我们就对原串位置以及倒过来后的相应位置做LCP。

poursoul 1168

URAL 1297 Palindrome 后缀数组 或 Manacher 求最长回文子串

题目大意: 就是给出一个长度不超过1000个只包含大小写英文字母的字符串,输出其最长回文子串 大致思路: 首先很容易想到用将该字符串本身反转之后与自己连接起来, 中间用一个未出现的字符隔开, 求出后缀数组 刚开始想的是枚举回文串长度,但是发现不能二分判断改长度是否就满足题意, 于是想到一个枚举起点和长度利用RMQ查询和对称性判断的O(n*n)的方法 不过这样并不是最好的枚举方法

Gatevin的专栏 854
上一篇: SPOJ - DISUBSTR 多少个不同的子串
下一篇: poj 2774 最长公共子串 后缀数组
Tyuio28
博客等级 码龄11年 12粉丝 279原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值