当前位置:网站首页>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;
}
边栏推荐
- F - Two Exam(AtCoder Beginner Contest 238)
- Graduation project of game mall
- 【Rust 笔记】16-输入与输出(上)
- Bit mask of bit operation
- Introduction and experience of wazuh open source host security solution
- API related to TCP connection
- leetcode-6108:解密消息
- Detailed explanation of expression (csp-j 2021 expr) topic
- leetcode-556:下一个更大元素 III
- Mysql database (I)
猜你喜欢
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
6. Logistic model
Sword finger offer 06 Print linked list from beginning to end
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
leetcode-6111:螺旋矩阵 IV
wordpress切换页面,域名变回了IP地址
Sword finger offer 04 Search in two-dimensional array
Sword finger offer 09 Implementing queues with two stacks
API related to TCP connection
随机推荐
[cloud native] record of feign custom configuration of microservices
Personal developed penetration testing tool Satania v1.2 update
F - Two Exam(AtCoder Beginner Contest 238)
Daily question 2013 Detect square
1.15 - 输入输出系统
Sword finger offer 06 Print linked list from beginning to end
剑指 Offer 58 - II. 左旋转字符串
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
Introduction to convolutional neural network
One question per day 2047 Number of valid words in the sentence
leetcode-6111:螺旋矩阵 IV
Implement a fixed capacity stack
Convolution neural network -- convolution layer
QQ电脑版取消转义符输入表情
Annotation and reflection
ALU逻辑运算单元
卷积神经网络——卷积层
js快速将json数据转换为url参数
leetcode-22:括号生成
Some common problems in the assessment of network engineers: WLAN, BGP, switch