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),而在处理奇偶问题上会比较繁琐,所以 ...
随机推荐
- Vue —— You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.问题
方法1: 在build/webpack.base.conf.js文件中,找到module->rules中有关eslint的规则,注释或者删除掉就可以了 module: { rules: [ // ...
- Dubbo源码学习总结系列三 dubbo-cluster集群模块
Dubbo集群模块的目的是将集群Invokers构造一个透明的Invoker对象,其中包含了容错机制.负载均衡.目录服务(服务地址集合).路由机制等,为RPC层提供高可用.高并发.自动发现.可治理的S ...
- linux 验证 NFS 是否成功
服务器端----->>客户端 1. 服务器端 [root@allentuns ~]# ifconfig |grep "Bcast" inet addr:192.168. ...
- 朴素贝叶斯算法——实现新闻分类(Sklearn实现)
1.朴素贝叶斯实现新闻分类的步骤 (1)提供文本文件,即数据集下载 (2)准备数据 将数据集划分为训练集和测试集:使用jieba模块进行分词,词频统计,停用词过滤,文本特征提取,将文本数据向量化 停用 ...
- 一、JQJson数组
叙述:常用的数据格式无非三种(组装数据,传参传值) 一.数组 : 1.定义 var select = []; //或 var select = new Array(); 2.JS给一个数组赋值 sel ...
- python的次方操作
好简单,不需要import任何包 b=a**n就是求a的n次方,如果n=0.5就是开方 如果开方的是负数或者附复数,需要 import math b=math.sqrt(a) 这样
- Android Release 打包提示 "错误:找不到符号"
搞了一上午....必须记录下来
- 对provide/inject的研究
1.组件中通过provider来提供变量,然后在子组件中通过inject来注入变量. 2.在父组件中定义: provide () { return { secondPageTableRoot: { t ...
- Centos6安装mysql
此处安装的是MariaDB,介绍如下: MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将My ...
- c++11 指针空值
1. 引入nullptr的必要性: 典型的指针初始化是将其指向一个空的位置.比如: int* my_ptr = 0; int* my_ptr = NULL; 一般情况下,NULL是一个宏定义. #un ...