L2-008 最长对称子串 (25 分)
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
不用算法也可以,可以用马拉车算法。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX 1000
using namespace std; char s[MAX + ];
char t[MAX * + ] = {'@','#'};
int p[MAX * + ];
int Manacher(char *str) {
int len = strlen(str);
for(int i = ;i <= len;i ++) {
t[i * ] = str[i - ];
t[i * + ] = '#';
}
t[len * + ] = ;
int mi = ,rpoint = ,mr = ;
for(int i = ;t[i];i ++) {
p[i] = i < rpoint ? min(rpoint - i,p[mi * - i]) : ;
while(t[i + p[i]] == t[i - p[i]]) p[i] ++;
if(i + p[i] > rpoint) {
mi = i;
rpoint = i + p[i];
}
if(mr < p[i]) mr = p[i];
}
return mr - ;
}
int main() {
int i = ;
while((s[i] = getchar()) != '\n') i ++;
s[i] = ;
printf("%d",Manacher(s));
}
L2-008 最长对称子串 (25 分)的更多相关文章
- L2-008 最长对称子串 (25 分) (模拟)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376 题目: 对给定的字符串,本题要求你输出 ...
- [刷题] PTA 7-64 最长对称子串
7-64 最长对称子串 我的代码: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 1001 4 5 int main ...
- 团体程序设计天梯赛-练习集L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- c语言:最长对称子串(3种解决方案)
问题描述: 输入一个字符串,输出该字符串中最大对称子串的长度.例如输入字符串:“avvbeeb”,该字符串中最长的子字符串是“beeb”,长度为4,因而输出为4. 解决方法:中序遍历 一,全遍历的方法 ...
- L2-008. 最长对称子串(思维题)*
L2-008. 最长对称子串 参考博客 #include <iostream> using namespace std; int main() { string s; getline(ci ...
- pat 团体赛练习题集 L2-008. 最长对称子串
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
- L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- PAT L2-008 最长对称子串(模拟字符串)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...
- 天梯赛L2-008 最长对称子串 (字符串处理)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
随机推荐
- 8188EU 在AM335X MC183上以AP+STA工作
[目的] 8188EU 在AM335X MC183上以AP+STA工作. [环境] 1. Ubuntu 16.04发行版 2. linux-3.2.0-psp04.06.00.11 3. MC1 ...
- 为什么使用 npm Scripts 构建项目
http://www.css88.com/archives/7025#more-7025 https://github.com/damonbauer/npm-build-boilerplate 这个我 ...
- UVa 11039 - Building designing 贪心,水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- java⑨
do-while,先执行一次,再判断! do{ 循环体 }while(循环条件); 经典案例: 1. 需求: 01.记录每次用户购买的商品金额! 之后进行 结账! 02.增加购买商品的数量 ...
- python 数据如何保存到excel中--xlwt
第一步:下载xlwt 首先要下载xlwt,(前提是你已经安装好了Python) 下载地址: https://pypi.python.org/pypi/xlwt/ 下载第二个 第二步:安装xl ...
- Jenkins持续集成web项目(七)
功能:用户开发完maven构建的web项目后,从本地提交代码到gogs远程仓库中,在执行 git commit 命令之前会先执行 maven配置的 findbugs插件,来检测项目是否有明显bug,如 ...
- xml解析与生成的学习资料
xml解析与生成的学习资料:http://blog.csdn.net/u012325167/article/category/6129813 ----------------------------- ...
- SQLite3 C/C++ 开发接口简介
SQLite3 C/C++ 开发接口简介 1.0 总览 SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的代码基础之上开发的,但是使用了和之前的版本不兼容的数据库格式和 ...
- 百度地图API示例:鼠标绘制点线面 控件修改
需求 :在使用地图API时,绘制工具栏控件想自己选择哪些要,哪些不要. 可以查看相应的类:官网地址: http://api.map.baidu.com/library/DrawingManager/1 ...
- mvc4使用百度ueditor编辑器
前言 配置.net mvc4项目使用ueditor编辑器,在配置过程中遇见了好几个问题,以此来记录解决办法.编辑器可以到http://ueditor.baidu.com/website/downloa ...