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中如何使用Servlet?

    第一种方法: 1.使用Servlet3的注解方式编写一个Servlet 2.在main方法的主类上添加注解: @ServletComponentScan(basePackages = "co ...

  2. 力扣119. 杨辉三角 II

    原题 1 class Solution: 2 def getRow(self, rowIndex: int) -> List[int]: 3 ans = [1] 4 for i in range ...

  3. 002-LED闪烁

    LED闪烁 功能:控制LED模块的小灯闪烁 #include<reg52.h> // 头文件 sbit LED = P2^0; // LED接低电平 void main() //主函数 { ...

  4. Vue.js 可排序列表 (Sortable & Searchable Tables) 组件

    可排序表格 (Sortable & Searchable Tables) 在网页和表单设计中非常常用.用户可以通过点击表头对将表格以该列做顺序或降序排列,也可以利用 Search Box 对表 ...

  5. Semaphore实战

    简介 Semaphore信号量计数器.和CountDownLatch,CyclicBarrier类似,是多线程协作的工具类,相对于join,wait,notify方法使用起来简单高效.下面我们主要看看 ...

  6. [同步到 MaixPy3 文档] 使用 Python 编程入门开源硬件项目

    本文是给有一点 Python 基础但还想进一步深入的同学,有经验的开发者建议跳过. 前言 在写这篇案例系列的时候 junhuanchen 期望能够引导用户如何成为专业的开发者,不是只会调用代码就好,所 ...

  7. RFC2544吞吐量测试详细步骤-MiniSMB-HurricaneII软件操作演示

    RFC2544吞吐量测试详细步骤-MiniSMB-HurricaneII软件操作演示 关键词:网络性能测试:RFC2544:吞吐量:吞吐率. RFC2544协议是RFC组织提出的用于评测网络互联设备( ...

  8. 反射的常用API

    反射的常用API 加载程序集 Assembly assembly = Assembly.Load("程序集名称"); // 从前目录加载程序集,提供程序集名称,无后缀 Assemb ...

  9. net5 中 后台任务利器之Hangfire 的使用

    什么是Hangfire Hangfire 是一个开源的.NET任务调度框架,目前1.6+版本已支持.NET Core.它最大特点在于内置提供集成化的控制台,方便后台查看及监控: 另外,Hangfire ...

  10. TensorFlow学习(1)-初识

    初识TensorFlow 一.术语潜知 深度学习:深度学习(deep learning)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法. 深度学 ...