Manacher(输出最长回文串及下标)
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
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.
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.
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.
a abcd
aza
No solution!
#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(输出最长回文串及下标)的更多相关文章
- Manacher(最长回文串)
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- Manacher 计算最长回文串
转自 http://blog.sina.com.cn/s/blog_3fe961ae0101iwc2.html 寻找字符串中的回文,有特定的算法来解决,也是本文的主题:Manacher算法,其时间复杂 ...
- Manacher算法,最长回文串
给你10000长度字符串,然你求最长回文字串,输出长度,暴力算法肯定超时 #include <iostream> #include <string> #include < ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- 字符串的最长回文串:Manacher’s Algorithm
题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...
- Manacher's Algorithm 马拉车算法(求最长回文串)
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...
- Hdu 3294 Girls' research (manacher 最长回文串)
题目链接: Hdu 3294 Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...
- manacher 算法(最长回文串)
manacher算法: 定义数组p[i]表示以i为中心的(包含i这个字符)回文串半径长 将字符串s从前扫到后for(int i=0;i<strlen(s);++i)来计算p[i],则最大的p[i ...
- Manacher模板(O(n)内求最长回文串长度)
转自:https://segmentfault.com/a/1190000008484167 /* 由于回文分为偶回文(比如 bccb)和奇回文(比如 bcacb),而在处理奇偶问题上会比较繁琐,所以 ...
随机推荐
- Windows10测试低版本IE方法
前端开发工程师可能了解IETester是一款IE多版本兼容性测试软件,但是只支持Windows Xp,Vista,7,8系统,Windows10是不支持的,网上所说的开启.net framework ...
- JavaScript中的垃圾收集机制
JavaScript 具有自动垃圾收集机制,也就是说,执行环境会负责管理代码执行过程中使用的内存. 在编写 JavaScript 程序时,开发人员不用再关心内存使用问题,所需内存的分配以及无用内存的 ...
- 2019-8-14-win10-使用-SMB-v1
title author date CreateTime categories win10 使用 SMB v1 lindexi 2019-08-14 08:55:55 +0800 2018-2-13 ...
- postmaster - PostgreSQL多用户数据库服务器
SYNOPSIS postmaster [ -A 0 | 1] [ -B nbuffers] [ -c name=value] [ -d debug-level] [ -D datadir] [ -F ...
- 动态规划—distinct-subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- CF671D Roads in Yusland
一道很玄妙的题= = 我们考虑先考虑DP 那么有$f[x]=min(c+\sum f[y])$ $f[x]$表示覆盖x的子树和x->fa[x]的所有边最小代价 我们枚举一条边c覆盖的x-> ...
- ORM大结局
1.添加记录: # 一对多的添加方式: #pub_obj=Publish.objects.filter(name="橙子出版社").first() # book=Book.obje ...
- OSS重磅推出OSS Select——使用SQL选取文件的内容
对象存储OSS(Object Storage Service)具有海量.可靠.安全.高性能.低成本的特点.OSS提供标准.低频.归档类型,覆盖多种数据从热到冷的存储需求,单个文件的大小从1字节到48. ...
- php str_word_count()函数 语法
php str_word_count()函数 语法 作用:计算字符串中的单词数.大理石平规格 语法:str_word_count(string,return,char) 参数: 参数 描述 strin ...
- Android中对Apk加固(加壳)续篇之---对Native层(so文件)进行加固
有人说Android程序用Java代码写的,再怎么弄都是不安全的,很容易破解的,现在晚上关于应用加固的技术也很多了,当然这些也可以用于商业发展的,梆梆加密和爱加密就是很好的例子,当然这两家加固的Apk ...