当前位置:网站首页>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;
}
边栏推荐
- Solution to the palindrome string (Luogu p5041 haoi2009)
- 【Rust 笔记】13-迭代器(中)
- Introduction et expérience de wazuh open source host Security Solution
- Kubedm series-00-overview
- Daily question 1342 Number of operations to change the number to 0
- One question per day 1447 Simplest fraction
- 全排列的代码 (递归写法)
- AtCoder Grand Contest 013 E - Placing Squares
- leetcode-6111:螺旋矩阵 IV
- Sword finger offer 06 Print linked list from beginning to end
猜你喜欢
Full Permutation Code (recursive writing)
CF1634 F. Fibonacci Additions
Appium基础 — 使用Appium的第一个Demo
LaMDA 不可能觉醒吗?
EOJ 2021.10 E. XOR tree
7. Processing the input of multidimensional features
【实战技能】非技术背景经理的技术管理
The connection and solution between the shortest Hamilton path and the traveling salesman problem
2017 USP Try-outs C. Coprimes
Appium自动化测试基础 — Appium测试环境搭建总结
随机推荐
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
卷积神经网络简介
全排列的代码 (递归写法)
Daily question 1984 Minimum difference in student scores
卷积神经网络——卷积层
Dynamic planning solution ideas and summary (30000 words)
Introduction to convolutional neural network
ALU逻辑运算单元
【Jailhouse 文章】Jailhouse Hypervisor
leetcode-6110:网格图中递增路径的数目
Bit mask of bit operation
Typical use cases for knapsacks, queues, and stacks
Graduation project of game mall
A problem and solution of recording QT memory leakage
每日一题-搜索二维矩阵ps二维数组的查找
Kubedm series-00-overview
Time of process
CF1637E Best Pair
The connection and solution between the shortest Hamilton path and the traveling salesman problem
[jailhouse article] performance measurements for hypervisors on embedded ARM processors