1040 Longest Symmetric String
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
题意:
找出最长的对称子串。
思路:
遍历字符串,以字符串中的每一个字符为对称中心,向两边查找。因为不知道对称子串的长度是奇数还是偶数,所以两种情况都要考虑,取最大值。
Code:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 int main() {
6 string str;
7 getline(cin, str);
8 int p1, p2, temp, ans = 1;
9 int len = str.length();
10 for (int i = 0; i < len; ++i) {
11 p1 = i;
12 p2 = i + 1;
13 temp = 0;
14 while (p1 >= 0 && p2 < len) {
15 if (str[p1--] == str[p2++])
16 temp += 2;
17 else
18 break;
19 }
20 if (temp > ans) ans = temp;
21 }
22 for (int i = 0; i < len; ++i) {
23 p1 = p2 = i;
24 temp = -1;
25 while (p1 >= 0 && p2 < len) {
26 if (str[p1--] == str[p2++])
27 temp += 2;
28 else
29 break;
30 }
31 if (temp > ans) ans = temp;
32 }
33 cout << ans << endl;
34 return 0;
35 }
1040 Longest Symmetric String的更多相关文章
- 1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...
- PAT 1040 Longest Symmetric String[dp][难]
1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...
- PTA (Advanced Level) 1040 Longest Symmetric String
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the lo ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1040 Longest Symmetric String
https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...
- 1040 Longest Symmetric String (25分)(dp)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT (Advanced Level) 1040. Longest Symmetric String (25)
暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ...
- PAT 1040 Longest Symmetric String
#include <cstdio> #include <cstdlib> using namespace std; ]; ]; int syslen(char str[], i ...
随机推荐
- Android7.0无需FileProvide搞定URI拍照、应用安装问题
根据官方文档,从Android7.0版本开始 使用URI打开或安装文件需要单独在应用里配置了,问了度娘,有好多版本的结果,个人认为最靠谱的就是下边这个方法,只需在application的oncreat ...
- Bootstrap下拉菜单、按钮式下拉菜单
1. 概述 下拉菜单使用频率也是比较高的,比较常见的使用场景是在导航菜单栏,某个主菜单含有下拉的子菜单. Bootstrap为下拉菜单提供了两种实现方式,即普通的下拉菜单还有按钮式的下拉菜单.我们先看 ...
- wxWidgets源码分析(4) - 消息处理过程
目录 消息处理过程 消息如何到达wxWidgets Win32消息与wxWidgets消息的转换 菜单消息处理 消息处理链(基于wxEvtHandler) 消息处理链(基于wxWindow) 总结 消 ...
- Nginx解析漏洞复现以及哥斯拉连接Webshell实践
Nginx解析漏洞复现以及哥斯拉连接Webshell实践 目录 1. 环境 2. 过程 2.1 vulhub镜像拉取 2.2 漏洞利用 2.3 webshell上传 2.4 哥斯拉Webshell连接 ...
- 推荐!!! Markdown图标索引网站
作者:三十三重天 博客: http://www.zhouhuibo.club 我们在观察别人的文章时候时,总能看到很多有趣的图标,像是这样
- 如何报告FreeBSD 的bug?
https://bugs.freebsd.org/bugzilla/ 注册个账号即可,请使用英语,把程序在不同程序上的运行结果列出来即可- 注意标记架构,如果有log还请一并附上,英语差可以 ...
- 源码解析之 Mybatis 对 Integer 参数做了什么手脚?
title: 源码解析之 Mybatis 对 Integer 参数做了什么手脚? date: 2021-03-11 updated: 2021-03-11 categories: Mybatis 源码 ...
- EmEditor, 在正则使用()匹配后 使用$1 $2进行对括号内的值进行引用
$1表示第一个括号,$2表示第二个括号,以此类推
- go-echarts 入门安装和使用
在 Golang 这门语言中,目前数据可视化的第三方库还是特别少,go-echarts的开发就是为了填补这部分的空隙.Echarts是百度开源的非常优秀的可视化图表库,凭借着良好的交互性,精巧的图表设 ...
- PTA 统计二叉树度为2的结点个数
6-4 统计二叉树度为2的结点个数 (11 分) 本题要求实现一个函数,可统计二叉树中度为2的结点个数. 函数接口定义: int NodeCount ( BiTree T); T是二叉树树根指针, ...