当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
数字图像处理(六)—— 图像压缩
急了,Mysql索引中最不容易记的三个知识点通透了
Acwing第 63 场周赛【未完结】
[Paper Reading] RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
智能指针学习笔记
PNAS最新研究:81%解题率,神经网络 Codex 推开高等数学世界大门
五、jmeter脚本的基本构成&断言
APICloud AVM wraps date and time selection components
R语言4.04安装教程
QCon 回顾 | Data Fabric:逻辑统一、物理分散
随机推荐
LeetCode_Binary Tree_Medium_515. Find the maximum value in each tree row
9. cuBLAS Development Guide Chinese Version--Configuration of Atomic Mode in cuBLAS
使用 FasterTransformer 和 Triton 推理服务器加速大型 Transformer 模型的推理
The realization of the salary slip issuing function of WeChat public account + web background
【数学模型】TOPSIS
[In-depth study of 4G/5G/6G topic-54]: L3 signaling control-3-segmentation of software functions and processes-signaling of CU-UP network elements
3531. 哈夫曼树
高数_证明_基本初等函数的导数公式
永续合约交易所系统开发逻辑详情
laravel - query builder 2
元宇宙医疗或将改变医疗格局
pdf导出工具类
Web3构架是怎么样的?
3 个开源项目,让你感受程序员的浪漫!
leetcode:294.翻转游戏
MySQL database
最稳定的淘宝商品详情接口
2022-08-08日报:Kaggle所有竞赛开源方案和Top思路汇总
MySQL 表的增删查改
国内部分手机游戏开始显示用户IP属地