http://acm.hdu.edu.cn/showproblem.php?pid=3294

Girls' research

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5711    Accepted Submission(s): 2117

Problem Description
One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First
step: girls will write a long string (only contains lower case) on the
paper. For example, "abcde", but 'a' inside is not the real 'a', that
means if we define the 'b' is the real 'a', then we can infer that 'c'
is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According
to this, string "abcde" changes to "bcdef".
Second step: girls will
find out the longest palindromic string in the given string, the length
of palindromic string must be equal or more than 2.
 
Input
Input contains multiple cases.
Each
case contains two parts, a character and a string, they are separated
by one space, the character representing the real 'a' is and the length
of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.
 
Output
Please execute the operation following the two steps.
If
you find one, output the start position and end position of palindromic
string in a line, next line output the real palindromic string, or
output "No solution!".
If there are several answers available, please choose the string which first appears.
 
Sample Input
b babd
a abcd
 
Sample Output
0 2
aza
No solution!
 
Author
wangjing1111
 
Source
 
Recommend
lcy
题意:先转换,再输出最长回文串及下标。
思路:利用原数组与加工后的数组的数量关系得到下标。
 
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#define INF 10000000
using namespace std;
char a[] , b[] , str[];
int p[]; int num; int main()
{
char c ;
while(~scanf("%c%s" , &c , a))
{
int len = strlen(a);
int q = c - 'a';
memset(str , , sizeof(str));
memset(p , , sizeof(p));
for(int i = ; i < len ; i++)
{
if(a[i] - q < 'a')
{
a[i] = a[i] + ;
}
a[i] = a[i] - q ;
}
//printf("%s\n" , a);
int l = ;
b[l++] = '$';
b[l++] = '#';
for(int i = ; i < len ; i++)
{
b[l++] = a[i];
b[l++] = '#';
}
int mx = - , mid , ans = ;
for(int i = ; i < l ; i++)
{
if(mx > i)
{
p[i] = min(p[*mid-i] , mx - i);
}
else
{
p[i] = ;
}
while(b[i-p[i]] == b[i+p[i]])
{
p[i]++;
}
if(mx < p[i]+i)
{
mid = i ;
mx = p[i] + i;
}
}
int index = , indey = ;
for(int i = ; i < l ; i++)
{
if(ans < p[i] - )
{
ans = p[i] - ;
index = i ; }
}
if(ans == )
{
printf("No solution!\n");
}
else
{
int r = (index + ans)/ - ;
int l = r - ans + ;
printf("%d %d\n" , l , r);
for(int i = l ; i <= r ; i++)
{
printf("%c" , a[i]);
}
printf("\n"); }
getchar();
} return ;
}

Manacher(输出最长回文串及下标)的更多相关文章

  1. Manacher(最长回文串)

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...

  2. Manacher 计算最长回文串

    转自 http://blog.sina.com.cn/s/blog_3fe961ae0101iwc2.html 寻找字符串中的回文,有特定的算法来解决,也是本文的主题:Manacher算法,其时间复杂 ...

  3. Manacher算法,最长回文串

    给你10000长度字符串,然你求最长回文字串,输出长度,暴力算法肯定超时 #include <iostream> #include <string> #include < ...

  4. Manacher算法 - 求最长回文串的利器

    求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...

  5. 字符串的最长回文串:Manacher’s Algorithm

    题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...

  6. Manacher's Algorithm 马拉车算法(求最长回文串)

    作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...

  7. Hdu 3294 Girls' research (manacher 最长回文串)

    题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...

  8. manacher 算法(最长回文串)

    manacher算法: 定义数组p[i]表示以i为中心的(包含i这个字符)回文串半径长 将字符串s从前扫到后for(int i=0;i<strlen(s);++i)来计算p[i],则最大的p[i ...

  9. Manacher模板(O(n)内求最长回文串长度)

    转自:https://segmentfault.com/a/1190000008484167 /* 由于回文分为偶回文(比如 bccb)和奇回文(比如 bcacb),而在处理奇偶问题上会比较繁琐,所以 ...

随机推荐

  1. Python之路-初识python及环境搭建与测试(Python安装、Anaconda安装、PyCharm安装)

    一.认识Python 起源 Python的作者是著名的“龟叔”Guido van Rossum,他希望有一种语言,这种语言能够像C语言那样,能够全面调用计算机的功能接口,又可以像shell那样,可以轻 ...

  2. ReactiveObjC(RAC)的使用汇总

    RAC 指的就是 RactiveCocoa ,是 Github 的一个开源框架,能够帮我们提供大量方便的事件处理方案,让我们更简单粗暴地去处理事件,现在分为 ReactiveObjC 和 Reacti ...

  3. VS2012在解决方案资源管理器显示解决方案名称

    工具>选项>项目与解决方案(需点右下方的显示所有)>点上 总是显示解决方案

  4. AOP拦截日志类,抛异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode

    AOP的日志拦截类中,抛出异常: java.lang.IllegalStateException: It is illegal to call this method if the current r ...

  5. console.log 不起作用

    devtool console.log 突然不起作用了

  6. centeros 安装maven 私服

    1:下载nexus: 下载maven: 2:解压缩 配置maven环境变量 cd /etc/profile MAVEN_HOME=/usr/mavenexport MAVEN_HOMEexport P ...

  7. 030:spaceless和autoescape 标签

    1.spaceless 标签: spaceless 标签:移除html标签中的空白字符.包括空格.tab键.换行等.示例代码如下: {% spaceless %} <p> <a hr ...

  8. 在vue项目中,解决如何在element表格中循环出图片列!

    效果图: 1,vue项目环境 2,引入element-ui组件 3,制作表格 此处省去制作循环表格数据那步,想看的可以找回我的博客:element中的表格处理:循环出表格数据 今天想在表格出循环出一列 ...

  9. c++11 默认函数的控制

    1. 类与默认函数: C++中声明自定义的类,编译器会默认生成未定义的成员函数: 构造函数 拷贝构造函数 拷贝赋值函数(operator=) 移动构造函数 移动拷贝函数 析构函数 编译器还会提供全局默 ...

  10. 【大量干货】史上最完整的Tengine HTTPS原理解析、实践与调试

    本文邀请阿里云CDN HTTPS技术专家金九,分享Tengine的一些HTTPS实践经验.内容主要有四个方面:HTTPS趋势.HTTPS基础.HTTPS实践.HTTPS调试. 一.HTTPS趋势 这一 ...