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. 行人重识别(ReID) ——数据集描述 Market-1501

    数据集简介 Market-1501 数据集在清华大学校园中采集,夏天拍摄,在 2015 年构建并公开.它包括由6个摄像头(其中5个高清摄像头和1个低清摄像头)拍摄到的 1501 个行人.32668 个 ...

  2. 采用pacemaker+corosync实现postgresql双机热备、高可用方案

    环境说明 参照上章已完成postgresql流复制配置,并关闭postgres服务. su - postgres pg_ctl -D /data/postgresql/data/ stop -m fa ...

  3. Tcp客户端构建流程

    tcp客户端构建流程 tcp的客户端要比服务器端简单很多,如果说服务器端是需要自己买手机.查手机卡.设置铃声.等待别人打电话流程的话,那么客户端就只需要找一个电话亭,拿起电话拨打即可,流程要少很多 示 ...

  4. 本地主机访问远程linux系统服务器上的jupyter notebook

    1,机器情况:服务器 centos python环境已经配置好了,在虚拟环境下安装了anaconda 并且在里面安装了jupyter notebook 2,主机是 windows     ipytho ...

  5. psexec远程重启服务器

    1 使受控机器支持远程psexec 管理 暂参照其他教程 2 打开远程命令行 3 重启服务执行net命令 停止远程桌面 net stop termservice 启动远程桌面 net start te ...

  6. tensor与数组转化

    import tensorflow as tfimg1 = tf.constant(value=[[[[1],[2],[3],[4]],[[1],[2],[3],[4]],[[1],[2],[3],[ ...

  7. AtCoder Beginner Contest 137

    前言 又是卡在了T4 , 总分 100 + 200 + 300 = 600pts rank 2449.以后还是要多积累比赛经验. A 输出a+b,a-b,a*b中最大的数,还要多简单?不挂代码了 B ...

  8. SpringBoot---异步消息

    1.概述 1.1.SpringBoot  对 JMS 的自动配置  位于  org.springframework.boot.autoconfigure.jms下: 1.2.SpringBoot  支 ...

  9. common pom

    <dependencies> <dependency> <groupId>com.github.pagehelper</groupId> <art ...

  10. C# 修改注册表立即刷新 转载

    修改注册表后不重启计算机并生效,代码如下:const int WM_SETTINGCHANGE = 0x001A; const int HWND_BROADCAST = 0xffff;IntPtr r ...