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 output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA
Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)

Tags: string algorithms  ( hide tags for unsolved problems )

Difficulty: 96    Printable version    Submit solution    Discussion (62)

My submissions    All submissions (19930)    All accepted submissions (6214)    Solutions rating (4095)

ac代码

首先肯定是要把原字符的逆序串接到原字符串的后面。然后便是从0~~len扫描这个字符串,如果第i个位置是一个回文的中心。然后求出i个i相应位置的lca的最大值就可以。每次枚举时要分奇偶两种情况考虑。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
using namespace std;
char str[3030];
int sa[3030],Rank[3030],rank2[3030],height[3030],c[3030],*x,*y,s[3030];
int n;
void cmp(int n,int sz)
{  int i;  memset(c,0,sizeof(c));  for(i=0;i<n;i++)  c[x[y[i]]]++;  for(i=1;i<sz;i++)  c[i]+=c[i-1];  for(i=n-1;i>=0;i--)  sa[--c[x[y[i]]]]=y[i];
}
void build_sa(int *s,int n,int sz)
{  x=Rank,y=rank2;  int i,j;  for(i=0;i<n;i++)  x[i]=s[i],y[i]=i;  cmp(n,sz);  int len;  for(len=1;len<n;len<<=1)  {  int yid=0;  for(i=n-len;i<n;i++)  {  y[yid++]=i;  }  for(i=0;i<n;i++)  if(sa[i]>=len)  y[yid++]=sa[i]-len;  cmp(n,sz);  swap(x,y);  x[sa[0]]=yid=0;  for(i=1;i<n;i++)  {  if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len])  x[sa[i]]=yid;  else  x[sa[i]]=++yid;  }  sz=yid+1;  if(sz>=n)  break;  }  for(i=0;i<n;i++)  Rank[i]=x[i];
}
void getHeight(int *s,int n)
{  int k=0;  for(int i=0;i<n;i++)  {  if(Rank[i]==0)  continue;  k=max(0,k-1);  int j=sa[Rank[i]-1];  while(s[i+k]==s[j+k])  k++;  height[Rank[i]]=k;  }
}
int minv[3010][20],lg[3030];
void init_lg()
{int i;lg[1]=0;for(i=2;i<2020;i++){lg[i]=lg[i>>1]+1;}
}
void init_RMQ(int n)
{int i,j,k;for(i=1;i<=n;i++){minv[i][0]=height[i];}for(j=1;j<=lg[n];j++)  {  for(k=0;k+(1<<j)-1<=n;k++)  {  minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);   }  }
}
int lcp(int l,int r)
{l=Rank[l];r=Rank[r];if(l>r)swap(l,r);l++;int k=lg[r-l+1];return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{while(scanf("%s",str)!=EOF){int n=0,i;int len=strlen(str);for(i=0;i<len;i++){s[n++]=str[i];}s[n++]=127;for(i=len-1;i>=0;i--)s[n++]=str[i];s[n]=0;build_sa(s,n+1,128);getHeight(s,n);init_lg();init_RMQ(n);int ans=-1,st;for(i=0;i<len;i++){int temp=lcp(i,n-i-1);if(temp*2-1>ans){ans=temp*2-1;st=i-temp+1;}temp=lcp(i,n-i);if(temp*2>ans){ans=temp*2;st=i-temp;}}//    printf("%d %d\n",ans,st);if(ans==1)printf("%c\n",str[0]);else{for(i=0;i<ans;i++){printf("%c",str[i+st]);}printf("\n");}}
}

URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)相关推荐

  1. Manacher马拉车算法求最长回文子串

    终于把马拉车算法搞明白了!赶紧记录一下. 这个算法用于查找一个字符串的最长回文子串 马拉车算法依次给数组p[i]赋值,马拉车算法的本质就是在每次给数组p[i] 赋值时尝试进行偷懒 例如,当要给p[6] ...

  2. 马拉车算法(manacher)求最长回文子串

    关于回文字符串的概念大家可以大致去搜索一下,这里不赘述. 一.解题思路 当前字符串 最长回文子串: 思路实际上很简单,就是遍历每一个元素,然后分别以这个元素为中心,向两边扩展,比如说现在i = 4,那 ...

  3. Manacher 求最长回文子串算法

    Manacher算法,是由一个叫Manacher的人在1975年发明的,可以在$O(n)$的时间复杂度里求出一个字符串中的最长回文子串. 例如这两个回文串"level"." ...

  4. python求回文_python实现求最长回文子串长度

    给定一个字符串,求它最长的回文子串长度,例如输入字符串'35534321',它的最长回文子串是'3553',所以返回4. 最容易想到的办法是枚举出所有的子串,然后一一判断是否为回文串,返回最长的回文子 ...

  5. mannachar(马拉车)求最长回文子串

    mannachar(马拉车)究竟是什么东西呢? 很简单,就是能让你在O(n)的复杂度内求出一个串的最长回文子串.传统的算法复杂度是O(n^2),呐,为什么mannachar能变快呢?因为mannach ...

  6. 字符串处理 —— 回文串相关 —— 求最长回文子串

    [暴力枚举] 求最长回文串最容易的方法就是暴力枚举,求出字符串的每一个子串,然后判断是不是回文,找到最长的那个回文串 求每一个子串的时间复杂度为 O(N^2),判断一个子串是不是回文时间复杂度为 O( ...

  7. URAL - 1297 Palindrome(后缀数组+RMQ)

    题目链接:点击查看 题目大意:给出一个字符串,求出最长的回文子串 题目分析:如果用以往的方法求,不可避免的都需要枚举所有子串,时间复杂度就已经到达了O(n),而后缀数组可以通过O(n)预处理后得到所有 ...

  8. 【HDU - 3068】最长回文(Manacher算法,马拉车算法求最长回文子串)

    题干: 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.  回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组case,不超过120组 ...

  9. 求最长回文子串——C++ (动态规划+暴力解法)

    声明:本文原题主要来自力扣,记录此博客主要是为自己学习总结,不做任何商业等活动! 一.题目描述 给你一个字符串 s,找到 s 中最长的回文子串. 示例 1: 输入:s = "babad&qu ...

最新文章

  1. mysql中正则表达式的用法_Mysql中正则表达式Regexp常见用法
  2. pandas 里面对nan的判断
  3. HttpURLConnection 发送http请求帮助类
  4. Mac OS X Terminal 101:终端使用初级教程以及Xcode
  5. c语言函数简单注释模板,C语言中的Doxygen注释模板
  6. 酷派手机android版本,酷派大神F2的手机系统是什么?能升级安卓4.3吗?
  7. fopen无法创建文件_Linux中一切皆文件,除了网卡
  8. sudo执行脚本找不到环境变量
  9. 手动搭建vue+node单页面(一)
  10. 二维随机变量期望公式_多维随机变量的特征数
  11. JS实现拖动滑块验证
  12. 省级c语言笔试题,C语言笔试题库.doc
  13. 22个月无休,华为36岁工程师在肯尼亚过劳猝死!
  14. 计算机工作原理与系统组成?,计算机工作原理及系统组成
  15. 特殊数字符号整理 - 圆圈数字
  16. 计算机那些事(8)——图形图像渲染原理
  17. 移动端开发-响应式页面
  18. JS逆向 | 某美食优惠聚合平台
  19. ps保存psd后图层全没了_怎么利用ps把psd图层单独导出为一张张的图片?
  20. DQN 从入门到放弃

热门文章

  1. java linux root权限管理_新的 Linux sudo 漏洞使本地用户获得 root 权限
  2. ov5640帧率配置_赛博朋克2077 优化设置大全!帧数50暴涨100
  3. shell学习(12)- jq
  4. GitLab-CI与GitLab-Runner
  5. oc随笔四:NSString、NSNumber
  6. js返回上一页并刷新
  7. 编程大讲坛、坛坛是佳酿--编程大讲坛:Visual Basic核心开发技术从入门到精通...
  8. CPU,寄存器,内存三者的关系
  9. .net remoting 与webservice
  10. IOS15打包静态库