当前位置:网站首页>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;
}
边栏推荐
- Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
- Introduction to convolutional neural network
- Maximum number of "balloons"
- 剑指 Offer 53 - II. 0~n-1中缺失的数字
- One question per day 1447 Simplest fraction
- Implement an iterative stack
- 剑指 Offer 53 - I. 在排序数组中查找数字 I
- 1.14 - 流水线
- Transform optimization problems into decision-making problems
- 927. 三等分 模拟
猜你喜欢
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
数据可视化图表总结(一)
[cloud native] record of feign custom configuration of microservices
6. Logistic model
Full Permutation Code (recursive writing)
QQ电脑版取消转义符输入表情
【Jailhouse 文章】Look Mum, no VM Exits
Introduction et expérience de wazuh open source host Security Solution
Sword finger offer 35 Replication of complex linked list
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
随机推荐
leetcode-556:下一个更大元素 III
剑指 Offer 09. 用两个栈实现队列
每日一题-无重复字符的最长子串
A misunderstanding about the console window
Simple knapsack, queue and stack with deque
ALU逻辑运算单元
2017 USP Try-outs C. Coprimes
剑指 Offer 05. 替换空格
How to adjust bugs in general projects ----- take you through the whole process by hand
Palindrome (csp-s-2021-palin) solution
【Rust 笔记】16-输入与输出(下)
【Rust 笔记】17-并发(下)
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
Sword finger offer 53 - ii Missing numbers from 0 to n-1
可变电阻器概述——结构、工作和不同应用
Common optimization methods
Daily question - longest substring without repeated characters
884. Uncommon words in two sentences
【Jailhouse 文章】Look Mum, no VM Exits
网络工程师考核的一些常见的问题:WLAN、BGP、交换机