hihoCoder第一周---最长回文子串(1032)
其实这就是mancher算法的板子题,贴个代码好了。
思想请见我的另一篇博客:
https://blog.csdn.net/qq_41090676/article/details/86768361
#include <cstdio>
#include <cstring>
const int maxn = 3e6;
int len1,len2,p[maxn],ans;
char s[maxn], str[maxn];
int min(int x,int y)
{
return x < y ? x : y;
}
void init()
{
memset(p, 0, sizeof(p));
str[0] = '$';
str[1] = '#';
for (int i = 0; i < len1;i++) {
str[i * 2 + 2] = s[i];
str[i * 2 + 3] = '#';
}
len2 = 2 * len1 + 2;
str[len2] = '*';
}
void Mancher()
{
int mx = 0, id = 0;
for (int i = 1; i < len2;i++) {
if (i<mx)
p[i] = min(p[id * 2 - i], mx - i);
else
p[i] = 1;
while (str[i-p[i]]==str[i+p[i]])
p[i]++;
if (p[i]+i>mx) {
mx = p[i] + i;
id = i;
}
}
}
int main()
{
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", s);
len1 = strlen(s);
init();
Mancher();
ans = 0;
for (int i = 1; i < len2;i++)
if (p[i]>ans)
ans = p[i];
printf("%d\n", ans - 1);
}
return 0;
}
hihoCoder第一周---最长回文子串(1032)的更多相关文章
- hiho一下 第一周 最长回文子串
时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...
- 【hiho一下】第一周 最长回文子串
题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...
- hiho一下第一周 最长回文子串
题目链接:http://hihocoder.com/contest/hiho1/problem/1 #include <iostream> #include <cstdio> ...
- hihocoder 第一周 最长回文字串
题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...
- hiho 第1周 最长回文子串
题目链接:http://hihocoder.com/problemset/problem/1032 #include <bits/stdc++.h> using namespace std ...
- hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)
题意:给一个字符串,求最长回文子串的长度. 思路: (1)暴力穷举.O(n^3) -----绝对不行. 穷举所有可能的出现子串O(n^2),再判断是否回文O(n).就是O(n*n*n)了. (2)记录 ...
- [hihoCoder] #1032 : 最长回文子串
时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...
- hihocoder #1032 : 最长回文子串 Manacher算法
题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...
- hihocoder #1032 : 最长回文子串【 manacher算法实现 】
#1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
随机推荐
- 学习Mahout(三)
开发+运行第一个Mahout的程序 代码: /** * Licensed to the Apache Software Foundation (ASF) under one or more * con ...
- Rsync备份同步数据工具
Rsync is a fast and extraordinarily versatile file copying tool. Rsync是一款开源的,快速的,多功能的,可实现全量和增量的本地 ...
- [題解]luogu_P1144最短路計數
1.無權圖最短路邊權為1 2.如果兩個點恰好不能被更新(d[y]==d[x]+1)那麼就能通過x的所有最短路到達y,所以ans[y]+=ans[x] 3.如果兩個點不能恰好被更新(d[y]>d[ ...
- VUE循环菜单
- C# 基础之构造函数
什么是构造函数? 构造函数主要用于创建类的实例对象,当调用一个构造函数创建对象时,构造函数会为对象分配内存空间并初始化类的成员. 构造函数分为:1.实例构造函数.2.静态构造函数 1.实例构造函数 使 ...
- 牛客寒假5-A.炫酷双截棍
链接:https://ac.nowcoder.com/acm/contest/331/A 题意: 小希现在手里有一个连着的两块木条,长度分别为l1l1,l2l2,木条之间有一个无摩擦的连接点,木条之间 ...
- (洛谷P2512||bzoj1045) [HAOI2008]糖果传递 || 洛谷P4016 负载平衡问题 || UVA11300 Spreading the Wealth || (洛谷P3156||bzoj3293) [CQOI2011]分金币
bzoj1045 洛谷P4016 洛谷P2512 bzoj3293 洛谷P3156 题解:https://www.luogu.org/blog/LittleRewriter/solution-p251 ...
- Guard Duty (hard) Codeforces - 958E3 || uva 1411
https://codeforces.com/contest/958/problem/E3 当没有三点共线时,任意一个这样的点集都是保证可以找到答案的,(考虑任意一种有相交的连线方案,一定可以将其中两 ...
- Azkaban的架构(三)
Azkaban是什么?(一) Azkaban的功能特点(二) 不多说,直接上干货! http://www.cnblogs.com/zlslch/category/938837.html Azkaban ...
- sql server 时间查询
select CONVERT(varchar, getdate(),8 ) --获取当前时间时分14:13:59 select CONVERT(varchar, getdate(),23 ) - ...