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

题目分析:看到题 想到了在动态规划中看到最大字串和(不过感觉并没有什么关系) 
但按着动态规划的想法做了下去 之前和朋友争论动态规划没有贪心的思想。。是我错了 对于这道题 我们选择

$dp[i]=前i个字符中最大的对称字串$

$dp[i]=\begin{cases}dp[i]=max(dp[i],dp[i-1]+2),& \text{if}\ s[i-1]=s[i-dp[i-1]-2] \\ dp[i]=max(dp[i],dp[i-1]+1), & \text{if}\ s[i-1]=s[i-dp[i-1]-1] \end{cases}$

$估计这也并不好理解  其实可以这么想 对于每一个dp[i]来说 我们更新它只有两种方式,一种是在dp[i-1]的基础上加两个字符 分别是 第i-1个字符与 i-dp[i-1]-2字符(不难算)  一种是在dp[i-1]的基础上只加第i-1个元素$

 #define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int dp[]; //dp[i]定义为前i个字符中最大对称字串 int main()
{
string s;
getline(cin, s);
for (int i = ; i <= s.length(); i++)
{
if (i - dp[i - ] - >= )
if (s[i - ] == s[i - dp[i - ] - ])
dp[i] =max(dp[i],dp[i - ] + );
if (i - dp[i - ] - >= )
if (s[i - ] == s[i - dp[i - ] - ])
dp[i] = max(dp[i], dp[i - ] + );
dp[i] = max(dp[i], );
}
int max = -;
for (int i = ; i <= s.length(); i++)
{
if (dp[i] > max)
max = dp[i];
}
cout << max;
}

1040 Longest Symmetric String (25分)(dp)的更多相关文章

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

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

  2. 【PAT甲级】1040 Longest Symmetric String (25 分)(cin.getline(s,1007))

    题意: 输入一个包含空格的字符串,输出它的最长回文子串的长度. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include<bits/std ...

  3. 1040. Longest Symmetric String (25)

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

  4. PAT1040 Longest Symmetric String (25分) 中心扩展法+动态规划

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

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

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

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

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

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

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

  8. pat1040. Longest Symmetric String (25)

    1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...

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

随机推荐

  1. 【图文+视频新手也友好】Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)

    目录 视频讲解: 一.数组的概述 二.一维数组的使用 三.Arrays工具类中的sort方法(sort方法用的多,我们具体讲一下) 四.数组中的常见异常 五.一维数组练习题 六.彩蛋(本期视频使用的P ...

  2. pytest、tox、Jenkins实现python接口自动化持续集成

    pytest介绍 pytest是一款强大的python测试工具,可以胜任各种级别的软件测试工作,可以自动查找测试用并执行,并且有丰富的基础库,可以大幅度提高用户编写测试用例的效率,具备可扩展性,用户自 ...

  3. ggplot2(10) 减少重复性工作

    10.1 简介 灵活性和鲁棒性的敌人是:重复! 10.2 迭代 last_plot()用于获取最后一次绘制或修改的图形. 10.3 绘图模板 gradient_rb <- scale_colou ...

  4. 基于 HTML5 Canvas 的拓扑组件 ToolTip 应用

    前言 ToolTip 效果是网页制作中常见的使用特效.当用户将鼠标悬浮在某个控件上时,ToolTip 显示并向用户展示相应的提示信息:当鼠标离开时,ToolTip 隐藏.一般情况下,我们使用 Tool ...

  5. [Bugku]Web题解

    bugku地址链接:https://ctf.bugku.com 1.web2 浏览器就显示一堆动态笑脸,时间长了密集恐惧症了. 解法1: F12查看源码 解法2: 地址栏输入: view-source ...

  6. 全国职业技能大赛信息安全管理与评估-一些细节tips

    Base64加解密: ubuntu@VM-0-5-ubuntu:~$ echo iloveyou | base64aWxvdmV5b3UKubuntu@VM-0-5-ubuntu:~$ echo aW ...

  7. ElasticSearch 32766 的限制

    插入一个稍大的数据到elasticsearch的时候,突然报错“ 400 - elastic: Error 400 (Bad Request): Document contains at least ...

  8. 文献名:Repeat-Preserving Decoy Database for False Discovery Rate Estimation in Peptide Identication (用于肽段鉴定中错误发生率估计的能体现重复性的诱饵数据库)

    文献名:Repeat-Preserving Decoy Database for False Discovery Rate Estimation in Peptide Identication (用于 ...

  9. java,jq,ajax写分页

    1.先写好html基础样式 我懒得去写css样式233,能看就行 <style> #page { width: 20px; } </style> <table> & ...

  10. 【翻译】如何使用 OpenVINO 来优化 OpenCV

    本文翻译自 Vishwesh Shrimali 的  "Using OpenVINO with OpenCV" 原文链接: https://www.learnopencv.com/ ...