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 ...
随机推荐
- jQuery的一些总结(持续更新中...)
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6693399.html 1. $.expr[':'] 过滤 ...
- 纠结的链接——ln、ln -s、fs.symlink、require
纠结的链接--ln.ln -s.fs.symlink.require 提交 我的留言 加载中 已留言 inode 我们首先来看看 linux 系统里面的一个重要概念:inode. 我们知道,文件存储在 ...
- [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
When subscribers create new "inner" sources and subscriptions, you run the risk of losing ...
- Office2010,PPT,EXCEL如何插入日历控件
1 在Office2010中插入其他控件,然后找到日历控件 2 十字架随便在Excel中绘制一下,得到一个日历控件,注意此时还是在设计模式下,在设计模式下日历控件不是正常状态,你还是可以双击这个控 ...
- Office WORD如何输入长下划线
选中一段文字,点击下划线按钮,可以添加下划线 同样,选中一段空格,点下划线,也可以添加下划线
- 微信小程序 自定义组件(stepper)
项目目录: 步骤一:创建组件 声明这一组文件为自定义组件 stepper.json { "component": true, "usingComponents" ...
- poj 1840 哈希
Eqs Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14093 Accepted: 6927 Description ...
- linux-shell脚本命令之awk
[ awk简单介绍: ] awk能够从一个文本中获取部分内容, 或者对这个文本进行排版, 使它按某种格式输出. [ awk工作流程: ] awk会把文件一行内容去到内存里, 然后对这行内容进行分段 ( ...
- Xcode中使用git
项目中添加git 也可在开始新建项目时勾选git,这是针对开始没有勾选git的情况 打开终端 cd 项目文件目录 //初始化一个代码仓库, git init //将当前目录及子目录中的文件标记为要添加 ...
- NS3网络仿真(9): 构建以太网帧
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在NS3使用了一个叫Packet的类来表示一个数据帧,本节尝试用它构造一个以太网帧. 以下是一个典 ...