当前位置:网站首页>1040 Longest Symmetric String
1040 Longest Symmetric String
2022-07-05 06:00: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++) { // Odd numbers
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;
}
边栏推荐
- Configuration and startup of kubedm series-02-kubelet
- Dynamic planning solution ideas and summary (30000 words)
- Simple knapsack, queue and stack with deque
- 【Jailhouse 文章】Jailhouse Hypervisor
- 剑指 Offer 06.从头到尾打印链表
- leetcode-6108:解密消息
- Solution to game 10 of the personal field
- js快速将json数据转换为url参数
- 每日一题-搜索二维矩阵ps二维数组的查找
- Maximum number of "balloons"
猜你喜欢
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
Implement an iterative stack
数据可视化图表总结(二)
个人开发的渗透测试工具Satania v1.2更新
leetcode-6111:螺旋矩阵 IV
剑指 Offer 53 - I. 在排序数组中查找数字 I
F - Two Exam(AtCoder Beginner Contest 238)
【Jailhouse 文章】Look Mum, no VM Exits
Appium自动化测试基础 — Appium测试环境搭建总结
Sword finger offer 05 Replace spaces
随机推荐
leetcode-556:下一个更大元素 III
How to adjust bugs in general projects ----- take you through the whole process by hand
Implement an iterative stack
CF1637E Best Pair
How many checks does kubedm series-01-preflight have
Sword finger offer 53 - ii Missing numbers from 0 to n-1
[jailhouse article] jailhouse hypervisor
Sword finger offer 05 Replace spaces
剑指 Offer 05. 替换空格
全排列的代码 (递归写法)
Sword finger offer 05 Replace spaces
leetcode-1200:最小绝对差
Sword finger offer 58 - ii Rotate string left
ALU逻辑运算单元
[practical skills] how to do a good job in technical training?
Daily question 1984 Minimum difference in student scores
卷积神经网络简介
Solution to the palindrome string (Luogu p5041 haoi2009)
Brief introduction to tcp/ip protocol stack
927. 三等分 模拟