当前位置:网站首页>L2-008 最长对称子串 (25 分)
L2-008 最长对称子串 (25 分)
2022-08-08 16:51:00 【Here_SDUT】
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
输入格式: 输入在一行中给出长度不超过1000的非空字符串。
输出格式: 在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
分析 数据很小,暴力求解即可,注意对称子串有奇数和偶数两种,分类讨论一下即可。
代码
// _ooOoo_
// o8888888o
// 88" . "88
// (| -_- |)
// O\ = /O
// ____/`---'\____
// . ' \| |// `.
// / \||| : |||// \
// / _||||| -:- |||||- \
// | | \\ - /// | |
// | \_| ''\---/'' | |
// \ .-\__ `-` ___/-. /
// ___`. .' /--.--\ `. . __
// ."" '< `.___\_<|>_/___.' >'"".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `-. \_ __\ /__ _/ .-` / /
// ======`-.____`-.___\_____/___.-`____.-'======
// `=---='
//
// .............................................
// 佛祖保佑 一发AC 永无BUG
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e5+10;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
typedef pair<int,int> PII;
int main(int argc, char const *argv[]) {
string s;
getline(cin,s);
int ans = 1;
int n = s.size();
for(int i = 1; i < n - 1; i++) {
if(s[i] == s[i-1]) {//没有对称中心,长度为偶数
int now = 1;
while(i-now-1 >= 0 && i + now < n && s[i-now-1] == s[i+now]) now++;
ans = max(ans, now*2);
}
else if(s[i-1] == s[i+1]) {//对称中心为s[i],长度为奇数
int now = 1;
while(i-now >= 0 && i + now < n && s[i-now] == s[i+now]) now++;
ans = max(ans, now * 2 - 1);
}
}
cout << ans <<endl;
return 0;
}
边栏推荐
- ASP.NET Core依赖注入之旅:4.体验服务的注册和消费
- Nervegrowold: machine advanced learning advice
- 【 8.7 】 source code - card to LCM with GCD 】 【 】
- 【数学模型】TOPSIS
- jupyter notebook hide & show all output
- phar反序列化
- Obtain - 64 [chances] : the soldier, subtlety also - 5 - read sun tzu - melee meter
- R语言(数值、列表、矩阵)上应用函数(sqrt、round、mean、log)、将矩阵所有数据求对数、就矩阵整体的均值、使用apply函数计算矩阵matrix的行均值、列均值、trim设置返回结果精度
- Are Huishang Futures official and reliable?Is it safe to open an account in Huishang Futures?
- 敏捷开发项目管理的一些心得
猜你喜欢
随机推荐
二、junit接口自动化框架之二次开发
字节一面:TCP 和 UDP 可以使用同一个端口吗?
【 8.7 】 source code - card to LCM with GCD 】 【 】
mysql 索引和 pgsql 索引 命名区别
bzoj1507 [NOI2003]Editor
ASP.NET Core依赖注入之旅:4.体验服务的注册和消费
laravel数据库: 查询构造器
bzoj1097 [POI2007]旅游景点atr
MySQL 数据库
ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]
10.cuBLAS开发指南中文版--cuBLAS中的logger配置
GHOST tool to access the database
Is it safe to open an account with CICC Wealth?How does it work?
PNAS最新研究:81%解题率,神经网络 Codex 推开高等数学世界大门
【论文阅读】RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
国内部分手机游戏开始显示用户IP属地
它们不一样!透析【观察者模式】和【发布订阅模式】
MVCC,主要是为了做什么?
mysql进阶(二十九)常用函数汇总
[深入研究4G/5G/6G专题-54]: L3信令控制-3-软件功能与流程的切分-CU-UP网元的信令