当前位置:网站首页>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;
}
边栏推荐
- Sword finger offer 09 Implementing queues with two stacks
- Daily question 2013 Detect square
- 数仓项目的集群脚本
- 2022 极术通讯-Arm 虚拟硬件加速物联网软件开发
- Configuration and startup of kubedm series-02-kubelet
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
- [article de jailhouse] jailhouse hypervisor
- 【Rust 笔记】15-字符串与文本(下)
- The sum of the unique elements of the daily question
- 【Rust 笔记】14-集合(上)
猜你喜欢
随机推荐
Control Unit 控制部件
【Rust 笔记】13-迭代器(中)
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Transform optimization problems into decision-making problems
1.14 - 流水线
Alu logic operation unit
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Introduction and experience of wazuh open source host security solution
A problem and solution of recording QT memory leakage
Daily question 1688 Number of matches in the competition
Light a light with stm32
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
leetcode-556:下一个更大元素 III
Implement a fixed capacity stack
【Rust 笔记】15-字符串与文本(下)
Fried chicken nuggets and fifa22
Daily question 1342 Number of operations to change the number to 0
【Jailhouse 文章】Jailhouse Hypervisor
Dynamic planning solution ideas and summary (30000 words)
Wazuh開源主機安全解决方案的簡介與使用體驗









