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

深入解析DDR5 RFM机制:从JESD79-5标准到硬件实现 本文深入解析了DDR5内存标准中的关键机制——RFM(Refresh Management)。文章从JESD79-5标准出发,详细阐述了RFM如何通过RAA计数器实现从被动定时到主动响应的智能刷新管理,并探讨了其硬件实现细节、命令调度挑战及系统级设计考量,为芯片设计工程师优化DDR5内存子系统性能与可靠性提供了实践指导。 阅读详情

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;
}


c# 开发串口调试助手 Visual Studio 2019 串口调试助手是用于在开发、测试和调试串口通信应用程序时进行串口数据的监视和交互的工具。 阅读详情

相关推荐

从选卡到验证:CST仿真GPU加速全流程实战指南

本文详细介绍了CST仿真中GPU加速的全流程实战指南,从硬件选型到软件配置,再到性能验证和常见问题排查。通过对比专业卡与游戏卡的性能差异,提供硬件选型建议,并分享驱动安装、关键设置及CST软件配置的黄金法则,帮助用户显著提升仿真效率。

weixin_30650039的博客 394

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

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

pxlsdz的博客 429

电气比例阀采用外置传感器和PID控制器实现化学机械抛光(CMP)的超高精度压力控制

摘要:为大幅度提高现有CMP工艺设备中压力控制的稳定性,在现有电气比例阀这种单回路PID压力调节技术的基础上,本文提出了升级改造方案,即采用串级控制法(双回路PID控制,也称级联控制),通过在现有电气比例阀回路中增加更高精度的压力传感器和PID控制器,可以将研磨抛光压力的稳定性提高一个数量级,从1~2%的稳定性提升到0.1~0.2%。

Shanghai_Eyoung的博客 1290

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

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

肘子的博客 1347

【移远EG800K-CN】从AT指令到MQTT实战:4G模块云端通信全解析

本文详细解析移远EG800K-CN 4G模块的AT指令应用与MQTT云端通信实战。从硬件连接到网络配置,再到MQTT协议的数据上报与命令接收,提供了完整的物联网通信解决方案,帮助开发者快速实现设备与云端的稳定数据交互。

o1p2q3r的博客 559

获取车辆VIN等OBD信息

获取车辆如下OBD信息: VIN:Vehicle Identification Number CALID:Calibration Identifications CVN:Calibration verification number 一、SAE J1939协议 通过请求 CALIBRATION INFORMATION (DM19),获取CVN和CALID。 发送请求:18EA00F9 00 D3 00 应答:18EC0000 10 14 00 03 FF 00 D3 00 数据,BYTE1=0x10,表明

jianxuezixuan的博客 1万+

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

AI Agent在营销领域的实战策略

本文从第一性原理出发,系统性拆解AI Agent在营销领域的落地逻辑、技术架构、实现方案与实战策略。全文覆盖从基础概念到高级优化的全层级知识:底层理论层面推导营销Agent的效用函数与决策框架;技术架构层面提出"感知-记忆-规划-行动-反思"五组件可落地架构;实践层面提供生产级代码实现、多行业案例与ROI提升方法论;前沿层面探讨多Agent协同、世界模型仿真等下一代技术方向。

操作系统内核探秘的博客 227

URAL - 1297 最长回文子串

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

兜率工的博客 307

第3讲:单端传输线的时域TDR仿真(基于实战的第一次仿真视角切换)

本文为CST仿真系列教程的第3讲,主要介绍传输线阻抗(TDR)的两种仿真方法:基于S参数的频域计算和基于高斯脉冲的时域计算。文章详细演示了在CST PCB Studio中修改电容参数、设置TDR仿真参数(包括截止时间和采样点数)的操作步骤,并通过对比实验展示TVS寄生电容对阻抗曲线的影响。最后将两种TDR方法的结果进行对比,验证了两种方法的一致性。本教程适合硬件工程师通过时域波形直观分析传输线阻抗特性,是信号完整性分析的重要技能。

一只豌豆象的博客 226

基于深度学习的航拍图像中地物变化检测

随着无人机技术的飞速发展,航拍图像成为获取地表信息的重要手段之一。然而,由于地球表面的不断变化,监测和识别航拍图像中的地物变化成为一项具有挑战性的任务。在人工智能领域,深度学习技术的兴起为地物变化检测提供了全新的解决方案。本文将介绍基于深度学习的航拍图像中地物变化检测的方法,并附上相应的代码实例。

一键难忘的博客 4175

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

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

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

Android应用发布到Google Play的步骤和注意事项

发布Android应用到Google Play是将您的应用分享给全球用户的重要步骤。Google Play是Android平台上最大的应用分发渠道之一,使得开发者可以轻松将他们的应用推广到数百万的用户。在本篇博客中,我们将逐步介绍如何将您的Android应用发布到Google Play,同时提供详细的步骤和注意事项,以及相关的代码示例。

2201_76125261的博客 850

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

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

anuancong1219的博客 193

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

【后缀数组】【最长回文子串】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

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【后缀数组|最长回文子串】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、付费专栏及课程。

余额充值