对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。

输入格式:

输入在一行中给出长度不超过1000的非空字符串。

输出格式:

在一行中输出最长对称子串的长度。

输入样例:

Is PAT&TAP symmetric?

输出样例:

11

思路一

由于字符串长仅1000,可以选择直接暴力找

思路二

套一下马拉车算法的模板即可

算法讲解:Here

Code Update:更新算法写法

// Murabito-B 21/04/23
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string Manacher(string s) {
int len = s.size();
if (len < 1) return s;
string ss;
// 预处理
for (int i = 0; i < len; ++i) {
ss += '#';
ss += s[i];
}
ss += '#';
len = ss.size();
int MaxR = 0; // 当前访问到的所有回文子串,所能触及的最右一个字符的位置
int pos = 0; // MaxRight对应的回文串的对称轴所在的位置
int MaxRL = 0; // 最大回文串的回文半径
int MaxPos = 0; // MaxRL对应的回文串的对称轴所在的位置
int *RL = new int[len]; // RL[i]表示以第i个字符为对称轴的回文串的回文半径
memset(RL, 0, len * sizeof(int));
for (int i = 0; i < len; ++i) {
//1) 当i在MaxRight的左边
if (i < MaxR) RL[i] = min(RL[2 * pos - 1], MaxR - i);
else // 2) 当i在MaxRight的右边
RL[i] = 1; while (i - RL[i] >= 0 && i + RL[i] < len && ss[i - RL[i]] == ss[i + RL[i]])
RL[i]++; // 更新MaxRight, pos
if (RL[i] + i - 1 > MaxR) {
MaxR = RL[i] + i - 1;
pos = i;
}
// 更新MaxRL, MaxPos
if (MaxRL <= RL[i]) {
MaxRL = RL[i];
MaxPos = i;
}
}
return s.substr((MaxPos - MaxRL + 1) / 2, MaxRL - 1);
}
void solve() {
string s;
getline(cin, s);
cout << Manacher(s).size() << "\n";
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
solve();
return 0;
}
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
string s;
char s_new[4000];
int p[5000];
int Init() {
int len = s.length();
s_new[0] = '$';
s_new[1] = '#';
int j = 2;
for (int i = 0; i < len; i++) {
s_new[j++] = s[i];
s_new[j++] = '#';
}
s_new[j] = '\0'; // 别忘了哦
return j; // 返回 s_new 的长度
} int Manacher() {
int len = Init(); // 取得新字符串长度并完成向 s_new 的转换
int max_len = -1; // 最长回文长度
int id;
int mx = 0;
for (int i = 1; i < len; i++) {
if (i < mx)
p[i] = min(p[2 * id - i],
mx - i); // 需搞清楚上面那张图含义, mx 和 2*id-i 的含义
else
p[i] = 1; while (s_new[i - p[i]] ==
s_new[i + p[i]]) // 不需边界判断,因为左有'$',右有'\0'
p[i]++; // 我们每走一步 i,都要和 mx 比较,我们希望 mx
// 尽可能的远,这样才能更有机会执行 if (i < mx)这句代码,从而提高效率
if (mx < i + p[i]) {
id = i;
mx = i + p[i];
}
max_len = max(max_len, p[i] - 1);
}
return max_len;
} int main() {
getline(cin, s);
printf("%d\n", Manacher());
return 0;
}

思路三

没想到可以使用DP做,思路来自网络

动态规划:

分析:有两种可能,⼀种是回⽂字符串的⻓度为奇数,⼀种是偶数的情况。i为字符串当前字符的下 标。

     当回⽂字串为奇数的时候,j表示i-j与i+j构成的回⽂字串⻓度;当回⽂字串⻓度为偶数的时候,j表示

     i+1左边j个字符⼀直到i右边j个字符的回⽂字串⻓度~

     ⽤maxvalue保存遍历结果得到的最⼤值并且输出~
#include <iostream>
using namespace std;
int main() {
string s;
getline(cin, s);
int maxvalue = 0, temp;
int len = s.length();
for (int i = 0; i < len; i++) {
temp = 1;
for (int j = 1; j < len; j++) {
if (i - j < 0 || i + j >= len || s[i - j] != s[i + j])
break;
temp += 2;
}
maxvalue = temp > maxvalue ? temp : maxvalue;
temp = 0;
for (int j = 1; j < len; j++) {
if (i - j + 1 < 0 || i + j >= len || s[i - j + 1] != s[i + j])
break;
temp += 2;
}
maxvalue = temp > maxvalue ? temp : maxvalue;
}
cout << maxvalue;
return 0;
}

L2-008 最长对称子串 (回文子串 / DP / Manacher算法)的更多相关文章

  1. HDU 4745 Two Rabbits(区间DP,最长非连续回文子串)

    Two Rabbits Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total ...

  2. 回文串的Manacher算法

    Manacher算法较传统算法的优化之处在于它对每个回文中心寻找回文半径的时候并不是都从半径为1开始找的,而是利用前面已经完成的任务,寻找一个初始的开始搜索的半径大小,复杂度是线性的. 参考博客:ht ...

  3. 最长回文串(manacher算法)

    ; ; int p[N]; char str[LEN], tmp[N]; //p[i]表示以str[i]为中心的回文往右延伸的 最长长度 void manacher(char* str, int* p ...

  4. UVA 11584 Partitioning by Palindromes 划分回文串 (Manacher算法)

    d[i]表示前面i个字符划分成的最小回文串个数, 转移:当第i字符加进来和前面区间j构成回文串,那么d[i] = d[j]+1. 要判断前面的字符j+1到i是不是回文串,可以用Manacher算法预处 ...

  5. 最长回文子串(Manacher算法)

    回文字符串,想必大家不会不熟悉吧? 回文串会求的吧?暴力一遍O(n^2)很简单,但当字符长度很长时便会TLE,简单,hash+二分搞定,其复杂度约为O(nlogn), 而Manacher算法能够在线性 ...

  6. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  7. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  8. 求最长回文子串:Manacher算法

    主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...

  9. [译+改]最长回文子串(Longest Palindromic Substring) Part II

    [译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...

  10. Manacher's algorithm: 最长回文子串算法

    Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abcc ...

随机推荐

  1. StackGres 数据库平台工程,使用 Citus + Patroni 创建生产级高可用分布式 PostgreSQL 分片集群

    系列 StackGres, 可私有部署的云原生数据库平台工程 StackGres 数据库平台工程功能介绍与快速上手 StackGres 1.6 数据库平台工程集群配置管理(K8S Pods/Postg ...

  2. 带你玩转 Vite + Vue3 高低版本常用玩法

    一.首先来个 Vite 的通用简介 Vite 是一种新型前端构建工具,在我们保险前端项目中已经推动并应用很久了,Vite 能够显著降低构建时间,提升前端开发效率. 它主要由两部分组成: 一个开发服务器 ...

  3. [复习随笔]python_dcgan网络复习小知识:模型定义

    定义参数 dataroot - the path to the root of the dataset folder. We will talk more about the dataset in t ...

  4. [ABC262C] Min Max Pair

    Problem Statement You are given a sequence $a = (a_1, \dots, a_N)$ of length $N$ consisting of integ ...

  5. net8获取泛微token以及访问api示例

    工作中涉及到调用泛微的场景,官方的示例又臭又长,抽空用NET8简化了写法,为了简化http访问,用了Flurl.Http这个库.在座各位大佬,我们直接就看代码了   using System.Secu ...

  6. django-celery-results - 使用 Django ORM/Cache 作为结果后端

    https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#django-celery-results-using-t ...

  7. MagicArray基本使用方法

    MagicArray致力于让研发不再卷,这个灵感来源于php语言,可能多少年以后,php可能不会有太多人记得.但是在一个年代里,如果论坛里里常见最火爆的帖子无疑是:php是世界上最好的编程语言.由此可 ...

  8. 三个月我迁移了100PB数据

    2023年马上结束,这一年做了不少事情,有一项我可以吹好几年,忍不住和大家分享一下啊. 需求 去年底收到一项需求,要求2个月内从某云存储迁移100PB数据到微软云存储,包含几百亿个文件.当时听到这个数 ...

  9. Python——第二章:集合 Set

    set集合,集合的特点是无序的 写法:s = {1,2,3} 但是如果内容是空的,它不是set集合,而是dict字典 s = {} print(type(s)) #输出结果 <class 'di ...

  10. 8.解决elasticsearch深度分页问题

    前面说到,分页可以使用from和size参数,类似于mysql的分页offset和limit.但是如果数据量比较大时,elasticsearch会对分页做出限制,因为此时会比较消耗性能. 为什么要限制 ...