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)相关推荐

  1. 1040. Longest Symmetric String (25)

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

  2. 1040. Longest Symmetric String (25)-PAT甲级真题

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

  3. 1040 Longest Symmetric String (25 分)_15行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a string, you are supposed to output the length of the long ...

  4. 1040 Longest Symmetric String (25 分)【难度: 一般 / 知识点: 最长回文子串】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 方法一: 最朴素的做法, 枚举所有的子串 # ...

  5. PAT 甲级 1040 Longest Symmetric String

    1040 Longest Symmetric String 题目大意:给出一个字符串,求出最长的回文子串的长度 思路:从第一个字符开始往后遍历,每次遍历第一个字符不动,从下一个字符开始找起,若是有回文 ...

  6. 1040 Longest Symmetric String

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

  7. 1040 Longest Symmetric String

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

  8. 1040 Longest Symmetric String 需再做

    解题思路 本题属于最长回文子串专题下.与之前的LIS和LCS的动规有两个较大的不同 1. 虽然最后也是要求长度,但是长度信息不再蕴含在dp数组当中,dp[i][j]表示的仅仅是从s[i]起s[j]止这 ...

  9. 浙大pat1040 Longest Symmetric String(25 分)

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

最新文章

  1. asp.net 防注入
  2. 归并排序以及三种常见优化
  3. 使用@functools.wraps的理由
  4. php mysql服务器配置_配置最新的PHP加MYSQL服务器
  5. 如何查看 SAP Fiori Elements List Report Table 都支持哪些设置
  6. 【Python】爬虫原理
  7. linux下的一些常见命令学习,学习猿地-Linux下的常见命令总结
  8. PHP框架剥离的判断是否为手机移动终端的函数
  9. 数据挖掘原理与算法_古今争翘,一首机器学习与数据挖掘神曲,共11.99G送你直上云霄...
  10. Java正则表达式全解
  11. 『软件工程7』详解软件项目管理之风险分析与管理
  12. stm32f407固件库移植ucosii_v2.92.07
  13. SQL server登录密码重置(忘记密码)
  14. h3c查在线计算机,H3C 交换机查看所有端口状态的命令
  15. centos8以后版本突然不能上网解决
  16. 【数据结构】直接插入排序以及时间复杂度的分析
  17. XGBOOST回归用法和官方参数解释
  18. 账号状态存储在MPT中的应用
  19. 5G NR学习理解系列——利用matlab工具生成5G NR信源
  20. java解析mdb文件_Access MDB文件解析查询,Access数据库解析工具类MdbUtils

热门文章

  1. 微信小程序实现抖音视频效果
  2. 处理仿真中存在多个timescale的问题
  3. 【安全】Web应用常见业务逻辑漏洞
  4. 谷歌图标gmmxie,是什么意思呢?
  5. python将图片裁剪成九宫格
  6. U盘越狱工具checkn1x更新发布1.1.7,支持iOS14.7 Beta版
  7. 巴伦变压器电路图_变压器使用(巴伦)
  8. 天然水晶挑选全攻略 真假水晶鉴别方法 保养净化
  9. Python3.9新特性
  10. php停车场系统设计,基于PHP的停车场管理系统设计与实现