PAT 1040 Longest Symmetric String (25)
1040 Longest Symmetric String (25)(25 分)
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
思路:
分别枚举子串开始的位置和子串的长度,需要注意的是子串对应位置下标的计算,假设开始位置为start,长度为len,与s[i]对应的字符是s[start+len-1-(i-start)],画个示意图很容易就算出来了。
代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;bool symmetric(string s, int start, int len)
{for (int i = start; i < start + len / 2; i++){if (s[i] != s[start + len - 1 - (i - start)])return false;}return true;
}int main()
{string s;getline(cin, s);int maxlen = 1;for (int i = 0; i < s.length(); i++){for (int j = 1; j <= s.length() - i; j++){if (symmetric(s, i, j) && j > maxlen)maxlen = j;}}cout << maxlen << endl;return 0;
}
PAT 1040 Longest Symmetric String (25)相关推荐
- 1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...
- 1040. Longest Symmetric String (25)-PAT甲级真题
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- 1040 Longest Symmetric String (25 分)_15行代码AC
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a string, you are supposed to output the length of the long ...
- 1040 Longest Symmetric String (25 分)【难度: 一般 / 知识点: 最长回文子串】
https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 方法一: 最朴素的做法, 枚举所有的子串 # ...
- PAT 甲级 1040 Longest Symmetric String
1040 Longest Symmetric String 题目大意:给出一个字符串,求出最长的回文子串的长度 思路:从第一个字符开始往后遍历,每次遍历第一个字符不动,从下一个字符开始找起,若是有回文 ...
- 1040 Longest Symmetric String
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the lo ...
- 1040 Longest Symmetric String
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- 1040 Longest Symmetric String 需再做
解题思路 本题属于最长回文子串专题下.与之前的LIS和LCS的动规有两个较大的不同 1. 虽然最后也是要求长度,但是长度信息不再蕴含在dp数组当中,dp[i][j]表示的仅仅是从s[i]起s[j]止这 ...
- 浙大pat1040 Longest Symmetric String(25 分)
1040 Longest Symmetric String(25 分) Given a string, you are supposed to output the length of the lon ...
最新文章
- SpringBoot中@ControlAdvice的使用
- 中国顶级 AI 研究者数量仅为美国 1/5:美国智库最新全球 AI 实力报告
- linux shell expr命令 字符串操作
- 一手好牌打的稀烂,如今面临倒计时,网友哭求众筹活下去...
- SSH框架整合的流程
- golang变量的数据类型:整型及其使用细节
- 开启协程_软件特攻队|GCC10发布,GCC 11将支持C++20协程功能?
- sv信道模型是什么_Unity Shader|基本光照模型实现中遇到的疑思
- NEUACM 2015年一月月赛
- 火狐浏览器插件(XPI 文件)签名指南
- 新版IAR软件打开旧版本创建的项目时出现的三个错误及其解决方案 is too long for segment definition
- 软件测试基础-概念(视频教程)
- 用强化学习来玩Atari游戏(基于Tensorflow的深度Q学习模型)
- 联想微型计算机Q150,联想Q150E电脑安装攻略
- 39、VS838红外线接收实验
- STM32—建立工程模板
- C++学习笔记:从C到C++之C++对C的加强
- 关于Android Studio单元测试中“Method d in android.util.Log not mocked.”问题的解决。
- 汽车功能安全 - 危险分析和风险评估
- excel文件损坏修复绝招_磁盘文件目录损坏如何修复——昨天我又学会了电脑的一招应用技巧...