题目链接:https://vjudge.net/problem/URAL-1297

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

题解:

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e4+; bool cmp(int *r, int a, int b, int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[i]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(j = ; j<=n; j <<= )
{
p = ;
for(i = n-j; i<n; i++) y[p++] = i;
for(i = ; i<n; i++) if(sa[i]>=j) y[p++] = sa[i]-j; for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[y[i]]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = ; x[sa[]] = ;
for(i = ; i<n; i++)
x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++; if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} int dp[MAXN][], mm[MAXN];
void initRMQ(int n, int b[])
{
mm[] = -;
for(int i = ; i<=n; i++)
dp[i][] = b[i], mm[i] = ((i&(i-))==)?mm[i-]+:mm[i-];
for(int j = ; j<=mm[n]; j++)
for(int i = ; i+(<<j)-<=n; i++)
dp[i][j] = min(dp[i][j-], dp[i+(<<(j-))][j-]);
} int RMQ(int x, int y)
{
if(x>y) swap(x, y);
x++;
int k = mm[y-x+];
return min(dp[x][k], dp[y-(<<k)+][k]);
} char str[MAXN];
int main()
{
scanf("%s" ,str);
int len = strlen(str);
int n = *len+;
for(int i = ; i<len; i++)
r[i] = r[n--i] = str[i];
r[len] = '$'; r[n] = ;
DA(r, sa, Rank, height, n, );
initRMQ(n, height); int st, ans = , lcp;
for(int i = ; i<len; i++)
{
lcp = RMQ(Rank[i], Rank[n--i]); //奇数,以i为中点匹配
if(*lcp->ans)
ans = *lcp-, st = i-lcp+; if(i==) continue;
lcp = RMQ(Rank[i], Rank[n-i]); //偶数, 逆串往后退一个位置进行匹配
if(*lcp>ans)
ans = *lcp, st = i-lcp;
} for(int i = st; i<st+ans; i++)
putchar(str[i]);
putchar('\n');
}

最新文章

  1. 怎样在asp中产生10个0-99的随机数
  2. sql server 分布式查询 和 主从服务器搭建
  3. jquery 新闻滚动效果
  4. 限制input输入类型(多种方法实现)
  5. 2. redis的数据类型
  6. 异步FIFO为什么用格雷码
  7. CFF前端沙龙总结
  8. easyui_tree 复选框 动态加载树
  9. css阴影
  10. PowerShell文件系统(一)前言
  11. Spring框架学习笔记(6)——bean的继承与依赖
  12. [POI2000] 最长公共子串
  13. jQuery移除或禁用html元素点击事件常用方法小结
  14. webpack.DefinePlugin
  15. Notes on Noise Contrastive Estimation and Negative Sampling
  16. [ES]ES查询指南
  17. tpcc-mysql 系列一:安装使用
  18. 2018-2019-2 网络对抗技术 20165324 Exp3:免杀原理与实践
  19. 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
  20. laravel前台html代码不显示

热门文章

  1. 2016.10.17 yaml文件里的labels和Pod、RC、Service的对应关系
  2. MySQL触发器 trigger学习
  3. java监控工具jstatd-windows
  4. 学写jQuery插件开发方法
  5. AWS上的游戏服务:Lumberyard + Amazon GameLift + Twitch
  6. uva 11885 - Number of Battlefields(矩阵高速幂)
  7. CSS3进度条 和 HTML5 Canvas画圆环
  8. Mongodb之备份恢复脚本
  9. Xenomai 3 POSIX
  10. HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程