题目链接:

  Hdu 3294  Girls' research

题目描述:

  给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.......,a == z。

问最长回文串所在区间,以及最长回文串所表示的明码。

解题思路:

  字符串长度[1,200000],用manacher算法很轻松就搞定了。

  get√新技能请点击me

 #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 最长回文串)的更多相关文章

  1. HDU 3068 最长回文 (Manacher最长回文串)

    Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等   Input 输 ...

  2. hdu----(4513)吉哥系列故事——完美队形II(manacher(最长回文串算法))

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  3. hdu 3294 Girls' research(manacher)

    Problem Description One day, sailormoon girls are so delighted that they intend to research about pa ...

  4. HDU 3294 Girls' research(manachar模板题)

    Girls' researchTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...

  5. hdu 3068 最长回文 (Manacher算法求最长回文串)

    参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...

  6. Manacher(输出最长回文串及下标)

    http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit: 3000/1000 MS (Java/Others ...

  7. Manacher算法 - 求最长回文串的利器

    求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...

  8. (最长回文串 模板) 最长回文 -- hdu -- 3068

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  9. 字符串的最长回文串:Manacher’s Algorithm

    题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...

随机推荐

  1. Meteor教程

    Meteor 是一个构建在 Node.js 之上的平台,用来开发实时网页程序.Meteor 程序位于数据库和用户界面之间,保持二者之间的数据同步更新. 因为 Meteor 是基于 Node.js 开发 ...

  2. Binder系列8—如何使用Binder(转)

    一.Native层Binder 源码结构: ClientDemo.cpp: 客户端程序 ServerDemo.cpp:服务端程序 IMyService.h:自定义的MyService服务的头文件 IM ...

  3. libevent HTTP client 的实现

    my_conn_ = evhttp_connection_base_new(ev_base_,ev_dns_,host,port); struct evhttp_request *http_req; ...

  4. 设计模式C++实现_2_简单工厂模式

    简单工厂模式 主要用于创建对象. 新加入类时. 不会影响曾经的系统代码. 核心思想是用一个工厂来依据输入的条件产生不同的类,然后依据不同类的 virtual 函数得到不同的结果. 以下以苹果手机的生产 ...

  5. java里int类型转byte类型

    今天在做书上的一个例子的时候, 要使用byte类型,首先我很直接的就写到了byte b = 0XAA, 结果报错, 说从int转换到byte可能会有损失. 我当时就很奇怪, 为什么会出现这种情况呢? ...

  6. Matplotlib绘图基础

    import matplotlib.pyplot as plt import numpy as np #绘图流程 x=np.linspace(-1,1,100) y=x**2 plt.plot(x,y ...

  7. web 界面设计---js设置txt值

    <head><meta http-equiv="Content-Type" content="text/html; charset=gb2312&quo ...

  8. 最简单的基于FFmpeg的移动端样例:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...

  9. [故障处理]西部数据wd elements xp 无法识别

    百度后,看到如下帖子,供需要的朋友参考,黑体字为本人修改添加: http://blog.sina.com.cn/s/blog_539747670102w62w.html 经咨询WD厂商(厂商电话800 ...

  10. apt-pkg

    1 什么是apt-pkg python的apt库,可以做apt可以做的任何事情. 2 apt_pkg.parse_depends(depends, strip_multiarch=True) 这里的d ...