Hdu 3294 Girls' research (manacher 最长回文串)
题目链接:
题目描述:
给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.......,a == z。
问最长回文串所在区间,以及最长回文串所表示的明码。
解题思路:
字符串长度[1,200000],用manacher算法很轻松就搞定了。
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std; typedef long long LL;
const int maxn = ;
char a[maxn], b[maxn*];
int vis[maxn*]; void manacher (char s[], int len)
{
int l = ;
b[l ++] = '$';
b[l ++] = '#';
for (int i=; i<len; i++)
{
b[l ++] = s[i];
b[l ++] = '#';
}
b[l] = ; /**
vis[i],以i为中心的回文串,向两端延伸的长度
mx为向后延伸最长的回文串延伸到的位置,id为其中心 **/ int mx = , id = , ans = , index;
for (int i=; i<l; i++)
{ /**
i < mx,因为[id-vis[id],mx],所以i与id-(i-id)对称
当i+vis[2*id-i] > mx时,vis[i] = mx - i; 当i >= mx....... 两端字符匹配时增加vis[i] 更新id 和 mx
**/ vis[i] = mx > i ? min (vis[*id-i], mx-i) : ;
while (b[i+vis[i]] == b[i-vis[i]]) vis[i] ++;
if (i + vis[i] > mx)
{
mx = i + vis[i];
id = i;
}
if (ans < vis[i])
{
ans = vis[i];
index = i;
}
} /**
每一个回文串都是以'#'为边界
vis[i]是以i为第一个字母,向两边延伸的长度
每一个字母在b数组中的偶数位置
**/ int x = index - vis[index] + ;
int y = index + vis[index] - ;
x = x / - ;
y = y / - ;
if (y != x)
{
printf ("%d %d\n", x, y);
for (int i=x; i<=y; i++)
printf ("%c", a[i]);
printf ("\n");
}
else
printf ("No solution!\n");
} int main ()
{
char ch[], vis[];
while (scanf ("%s %s", ch, a) != EOF)
{
int n = strlen (a); for (int i=; a[i]; i++)
{
a[i] -= ch[] - 'a';
if (a[i] < 'a')
a[i] += ;
} manacher(a, n);
}
return ;
}
Hdu 3294 Girls' research (manacher 最长回文串)的更多相关文章
- HDU 3068 最长回文 (Manacher最长回文串)
Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输 ...
- hdu----(4513)吉哥系列故事——完美队形II(manacher(最长回文串算法))
吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- hdu 3294 Girls' research(manacher)
Problem Description One day, sailormoon girls are so delighted that they intend to research about pa ...
- HDU 3294 Girls' research(manachar模板题)
Girls' researchTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- hdu 3068 最长回文 (Manacher算法求最长回文串)
参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...
- Manacher(输出最长回文串及下标)
http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit: 3000/1000 MS (Java/Others ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- (最长回文串 模板) 最长回文 -- hdu -- 3068
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- 字符串的最长回文串:Manacher’s Algorithm
题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...
随机推荐
- Python pandas学习笔记
参考文献:<Python金融大数据分析> #导入模块 import pandas as pd #生成dataframe df = pd.DataFrame([10,20,30,40], c ...
- Java 8中的时间
Java 8中的时间 学习了:https://blog.csdn.net/sun_promise/article/details/51383618
- HDU 3080 The plan of city rebuild(prim和kruskal)
The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 【转载】How browsers work--Behind the scenes of modern web browsers (前端必读)
浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工 作原理,我们将看到,从你在地址栏输入google.com到你看到google主页过程中都发生了什么. 将讨论的浏览器 今天,有五种主流浏览器- ...
- UVA 10288 - Coupons(概率递推)
UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...
- VUE组件如何与iframe通信问题
vue组件内嵌一个iframe,现在想要在iframe内获取父vue组件内信息,由于本人技术有限,采用的是H5新特性PostMessage来解决跨域问题. postMessage内涵两个API: on ...
- URL传参中文乱码的一种解决方法
中文乱码是由于,发送和接收方使用的编码解码格式不一致导致,以下是关于url传参解决中文乱码的一种方法,最后根据各种编码格式尝试解码,发现正确的解码格式 string strQueryString = ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心
E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x = ...
- 滑动窗体的最大值(STL的应用+剑指offer)
滑动窗体的最大值 參与人数:767时间限制:1秒空间限制:32768K 通过比例:21.61% 最佳记录:0 ms|8552K(来自 ) 题目描写叙述 给定一个数组和滑动窗体的大小.找出全部滑动窗体里 ...
- Genymotion设置网络桥接
1,打开Genymotion,找到对应的模拟器,点击“设置”按钮 2,在网络选项中选择桥接 Bridge