当前位置:网站首页>1040 Longest Symmetric String
1040 Longest Symmetric String
2022-07-05 05:51:00 【Brosto_Cloud】
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?
, the longest symmetric sub-string is s PAT&TAP s
, hence you must output 11
.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string s;
int ans, maxx = -1;
getline(cin, s);
for (int i = 0; i < s.size(); i++) { //奇数情况
int j = i - 1, k = i + 1;
ans = 1;
while (j >= 0 && k < s.size() && s[j] == s[k]) {
ans += 2;
j--;
k++;
}
maxx = max(maxx, ans);
}
for (int i = 0; i < s.size(); i++) {
int j = i, k = i + 1;
ans = 0;
while (j >= 0 && k < s.size() && s[j] == s[k]) {
ans += 2;
j--;
k++;
}
maxx = max(maxx, ans);
}
cout << maxx;
return 0;
}
边栏推荐
- Codeforces Round #716 (Div. 2) D. Cut and Stick
- PC register
- Wazuh開源主機安全解决方案的簡介與使用體驗
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- Dichotomy, discretization, etc
- Detailed explanation of expression (csp-j 2021 expr) topic
- 对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
- [article de jailhouse] jailhouse hypervisor
- 【Jailhouse 文章】Look Mum, no VM Exits
- 剑指 Offer 09. 用两个栈实现队列
猜你喜欢
剑指 Offer 04. 二维数组中的查找
剑指 Offer 05. 替换空格
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
Typical use cases for knapsacks, queues, and stacks
Appium基础 — 使用Appium的第一个Demo
1.13 - RISC/CISC
Light a light with stm32
Smart construction site "hydropower energy consumption online monitoring system"
Personal developed penetration testing tool Satania v1.2 update
6. Logistic model
随机推荐
2017 USP Try-outs C. Coprimes
剑指 Offer 53 - I. 在排序数组中查找数字 I
Flutter Web 硬件键盘监听
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
leetcode-6111:螺旋矩阵 IV
全排列的代码 (递归写法)
CF1634E Fair Share
F - Two Exam(AtCoder Beginner Contest 238)
Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
中职网络安全技能竞赛——广西区赛中间件渗透测试教程文章
Sword finger offer 35 Replication of complex linked list
PC register
Wazuh开源主机安全解决方案的简介与使用体验
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Light a light with stm32
LeetCode 1200.最小绝对差
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
Common optimization methods
leetcode-556:下一个更大元素 III
AtCoder Grand Contest 013 E - Placing Squares