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的更多相关文章

  1. 1040. Longest Symmetric String (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...

  2. PAT 1040 Longest Symmetric String[dp][难]

    1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...

  3. 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 ...

  4. PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)   Given a string, you are supposed to output the length of the ...

  5. PAT 甲级 1040 Longest Symmetric String

    https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...

  6. 1040 Longest Symmetric String (25分)(dp)

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...

  7. PAT甲题题解-1040. Longest Symmetric String (25)-求最长回文子串

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  8. PAT (Advanced Level) 1040. Longest Symmetric String (25)

    暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ...

  9. PAT 1040 Longest Symmetric String

    #include <cstdio> #include <cstdlib> using namespace std; ]; ]; int syslen(char str[], i ...

随机推荐

  1. SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理

    SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...

  2. 微信小程序警告设置 enable-flex 属性以使 flexbox 布局生效的解决办法

    微信小程序警告设置 enable-flex 属性以使 flexbox 布局生效的解决办法 具体情况: scroll-view 滚动,设置 display:flex 不生效并警告设置 enable-fl ...

  3. 看完我的笔记不懂也会懂----MarkDown使用指南

    目录 语法 [TOC] 自动生成目录 1. 标题 2. 文本强调 3. 列表 4. 图片 5. 超链接 6. 文本引用 7. 分割线 8. 代码 9. 任务列表 (MPE专属) 10. 表格 11. ...

  4. RocketMQ(4.8.0)——Broker 的关机恢复机制

    Broker 的关机恢复机制 一.Broker关机恢复概述 Broker关机恢复是指恢复 CommitLog.Consume Queue.Index File 等数据文件.Broker 关机分为正常调 ...

  5. MySQL提权 通过UDF

    目录 UDF是什么 命令执行 文本写入 Example: 远程写入 反弹Shell 提权 UDF是什么 参考:https://www.cnblogs.com/litlife/p/9030673.htm ...

  6. ng-class动态类几种用法

    方法1.逻辑在后面的中括号里面 ng-class="{true : 'checker disabled',false : 'checker' }[selectAllButton]" ...

  7. 六. SpringCloud网关

    1. Gateway概述 1.1 Gateway是什么 服务网关还可以用Zuul网关,但是Zuul网关由于一些维护问题,所以这里我们学习Gateway网关,SpringCloud全家桶里有个很重要的组 ...

  8. 学习版pytest内核测试平台开发万字长文入门篇

    前言 2021年,测试平台如雨后春笋般冒了出来,我就是其中一员,写了一款pytest内核测试平台,在公司落地.分享出来后,有同学觉得挺不错,希望能开源,本着"公司代码不要传到网上去,以免引起 ...

  9. redis过期key监听事件

    目录 redis安装 docker拉取 启动 redis 配置 命令监听 问题 程序监听 具体监听类 效果 总结 redis常用语缓存操作,但是redis功能不仅仅于此.今天我们来看看redis的ke ...

  10. scrapy框架爬取图片并将图片保存到本地

    如果基于scrapy进行图片数据的爬取 在爬虫文件中只需要解析提取出图片地址,然后将地址提交给管道 配置文件中:IMAGES_STORE = './imgsLib' 在管道文件中进行管道类的制定: f ...