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. 项目测试中发现产品bug怎么办

    我所在的产品线,并非公司最大最强的产品 甚至为了推广我们这个产品,一般会拿给客户先免费试用 而在试用之前,是要经过一番通测的,测得很急,测得很快 所以产品bug非常多 那么在测试项目的时候,自然会发现 ...

  2. R语言实战(二) 创建数据集

    2.1 数据集的概念 不同的行业对于数据集的行和列叫法不同.统计学家称它们为观测(observation)和变量(variable),数据库分析师则称其为记录(record)和字段(field),数据 ...

  3. String的那些事

    目录 String的特点? String的构造方法? String的常用方法? String的特点? 通过查看String类的源码我们得知:String类被final关键字修饰,这即是说明String ...

  4. js 面向对象 模拟日历

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Git 基础入门

    目录 git安装 基本设置 创建版本库 相关概念 将代码提交到分支仓库 版本回退 代码修改 撤销修改 文件删除 github 远程仓库 添加远程仓库 克隆远程仓库 分支操作 忽略特殊文件 git安装 ...

  6. Java多线程并发04——合理使用线程池

    在此之前,我们已经了解了关于线程的基本知识,今天将为各位带来,线程池这一技术.关注我的公众号「Java面典」了解更多 Java 相关知识点. 为什么使用线程池?线程池做的工作主要是控制运行的线程的数量 ...

  7. Kafka的面试题

    1.Kafka的设计是什么样的呢?Kafka将消息以topic为单位进行归纳将向Kafka topic发布消息的程序成为producers.将预订topics并消费消息的程序成为consumer.Ka ...

  8. Nacos作为微服务注册中心,爱不释手的感觉

    我觉得Nacos用起来还不错 在使用SpringCloud做分布式微服务架构时,注册中心是必不可少的一个组件.目前可以用的主要有:Eureka.Consul.Zookeeper.今天,我们就来说一下A ...

  9. Swift 4.0 数组(Array)学习

    定义数组常量(常量只有读操作) let array1: [Int] = [11, 55, 5] let array2 = [11, 55, 5] 定义数组变量 var array: [Int] = [ ...

  10. 【简说Python WEB】Web应用部署

    目录 [简说Python WEB]Web应用部署 应用层 缓存层 数据层 Gunicorn 的应用 1.安装Gunicorn 2.Gunicorn的启动 Nginx 的应用 1.docker方式部署安 ...