题目链接:

http://acm.hdu.edu.cn/showproblem.php?

pid=5371

题意:

给出一个长度为n的串,要求找出一条最长连续子串。这个子串要满足:1:能够平均分成三段,2:第一段和第三段相等,3:第一段和第二段回文。求最大子串的长度。

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string.h> using namespace std; const int N = 1100550; int n, l, tmp;
int p[2 * N];//记录回文半径
int str0[N];//原始串
int str[2 * N];//转换后的串 void init()
{
int i;
str[0] = -2; str[1] = -1;
l = 2;
for (i = 0, l = 2; i<n; i++, l += 2)
{
str[l] = str0[i];
str[l + 1] = -1;
}
str[l] = -5;
} int solve()
{
int i, mx, id;
mx = 0;//mx即为当前计算回文串最右边字符的最大值
for (i = 1; i < l; i++)
{
if (mx>i)
p[i] = p[2 * id - i]>(mx - i) ? (mx - i) : p[2 * id - i];
else
p[i] = 1;//假设i>=mx,要从头開始匹配
while (str[i + p[i]] == str[i - p[i]])
p[i]++;
if (i + p[i]>mx)//若新计算的回文串右端点位置大于mx。要更新po和mx的值
{
mx = i + p[i];
id = i;
}
} int ans = 0;
for (int i = 1; i < l; i += 2)
for (int j = i + p[i] - 1; j - i > ans; j -= 2)
{
if (p[j] >= j-i+1 && ans < j - i)
{
ans = j - i;
break;
}
} return ans / 2 * 3;
}
int main()
{
int t;
int cases = 1;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n); for (int i = 0; i < n; i++)
scanf("%d",&str0[i]);
init();
printf("Case #%d: %d\n", cases++, solve());
}
return 0;
}

hdu 5371 Hotaru&#39;s problem【manacher】的更多相关文章

  1. HDU 5371 Hotaru&#39;s problem(Manacher算法+贪心)

    manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成, ...

  2. HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. hdu 3068 最长回文【manacher】(模板题)

    <题目链接> 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如ab ...

  4. HDU - 3068 最长回文 【Manacher】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3068 思路 n^3 的做法 对于每个字符 同时 往左往右搜 但是要分奇偶 就是 n^3 n^2 的做法 ...

  5. HDU 5572 An Easy Physics Problem【计算几何】

    计算几何的题做的真是少之又少. 之前wa以为是精度问题,后来发现是情况没有考虑全... 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意: ...

  6. Hdu 5371 Hotaru's problem (manacher+枚举)

    题目链接: Hdu 5371 Hotaru's problem 题目描述: 给出一个字符串N,要求找出一条N的最长连续子串.这个子串要满足:1:可以平均分成三段,2:第一段和第三段相等,3:第一段和第 ...

  7. HDU 5894 hannnnah_j’s Biological Test【组合数学】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5894 题意: 一个圆桌上有$n$个不同的位置,$m$个相同的人安排到这$n$个位置上,要求两人相邻的 ...

  8. HDU 5371(2015多校7)-Hotaru&#39;s problem(Manacher算法求回文串)

    题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...

  9. HDU 5371 (2015多校联合训练赛第七场1003)Hotaru&#39;s problem(manacher+二分/枚举)

    pid=5371">HDU 5371 题意: 定义一个序列为N序列:这个序列按分作三部分,第一部分与第三部分同样,第一部分与第二部分对称. 如今给你一个长为n(n<10^5)的序 ...

随机推荐

  1. codeforces-505B

    题目连接:http://codeforces.com/contest/505/problem/B B. Mr. Kitayuta's Colorful Graph time limit per tes ...

  2. 51nod 1432 独木舟【贪心】

    1432 独木舟 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两 ...

  3. 洛谷 P3955 图书管理员【模拟/思维】

    题目描述 图书馆中每本书都有一个图书编码,可以用于快速检索图书,这个图书编码是一个 正整数. 每位借书的读者手中有一个需求码,这个需求码也是一个正整数.如果一本书的图 书编码恰好以读者的需求码结尾,那 ...

  4. ESLint 的使用和.eslintrc.js配置

    在团队协作中,为避免低级 Bug.产出风格统一的代码,会预先制定编码规范.使用 Lint 工具和代码风格检测工具,则可以辅助编码规范执行,有效控制代码质量. ESLint 简介 ESLint 由 Ja ...

  5. ThinkPHP的自动验证常用的正则

    ThinkPHP的自动验证常用的正则   ThinkPHP的自动验证机制是为了进行表单数据验证,验证可以支持function. callback.confirm.equal.unique和regex, ...

  6. Spark-submit脚本解读

    #!/usr/bin/env bash # # Licensed to the Apache Software Foundation (ASF) under one or more # contrib ...

  7. VUE -- 用组件上传文件和用xmlrequest上传

    xmlrequest: sendForm(str, types) { var form = this.$refs.ipas_form; var oOutput = document.querySele ...

  8. numpy自动生成数组

    1 np.arange(),类似于range,通过指定开始值,终值和步长来创建表示等差数列的一维数组,注意该函数和range一样结果不包含终值. >>> np.arange(10) ...

  9. hdu1008(c++)

    分清上升停留下降一步步来就是了 #include<iostream>#include<vector>using namespace std;int main(){ int N, ...

  10. homebrew代理设置

    方法一 brew用curl下载,所以给curl挂上socks5的代理即可. 在~/.curlrc文件中输入代理地址即可. socks5 = "127.0.0.1:1080" 方法二 ...