1040 Longest Symmetric String (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

解题思路:
  本题给出一行字符这里记为text,要求输出改行字符中最大回文串的长度。这里采用动态规划做法,用布尔数组dp[ bg ][ ed ]记录下标 bg ~ ed 组成的子串是否为回文串。设长度为1的字符串为回文串,在初始化dp数组是可以将长度为1与长度为2的字符串是否为回文串都标记完成。

  对任意一个字符串都有两种情况:

  1、是回文串,其对应dp为true;

  2、不是回文串,其对应dp为false;

  判断一个字符串是否为回文串只需要判断其首尾字符是否相等与其除首尾字符外的子串是否为回文串即可,这就转化为了求dp[bg + 1][ ed - 1],和判断text[ bg ]与text[ ed ]是否相等的问题。

  动态转移方程:dp[bg][ed] = 1(text[ bg ] ==  text[ ed ],dp[bg + 1][ ed - 1 ] == true)

之后只要遍历所有子串长度,遍历所有起始下标,并计根据其实下标与子串长度计算出末尾下标,根据动态转移方程即可求出答案。

 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3+;
bool dp[maxn][maxn] = {false}; //默认所有字符串都不是回文
string text; //text为出入的字符串
int main()
{
getline(cin, text);
int len = text.size();
int ans = ;
for(int i = ; i < len; i++){
dp[i][i] = true; //标记长度为1的字符串为回文串
ans = ; //最大回文串长度为1
if(i < len - && text[i] == text[i + ]){ //如果有两个连续字符
//(长度为2的回文串只有两个连续字符这种情况)
dp[i][i + ] = ; //标记其组成的字符串为回文串
ans = ; //最大回文串长度为2
}
}
for(int tempLen = ; tempLen <= len; tempLen++){ //遍历子串长度长度从3 ~ len的
for(int bg = ; bg + tempLen - < len; bg++){
//遍历起始位置,末尾下标为起始位置+当前子串长度-1,末尾下标不能达到给定字符串末位
int ed = bg + tempLen - ;
if(text[bg] == text[ed] && dp[bg + ][ed - ] == true){ //动态转移方程
dp[bg][ed] = ;
ans = tempLen; //记录答案
}
}
}
printf("%d\n", ans);
return ;
}

PTA (Advanced Level) 1040 Longest Symmetric String的更多相关文章

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

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

  2. 1040. Longest Symmetric String (25)

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

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

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

  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. 1040 Longest Symmetric String

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

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

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

  9. PAT 1040 Longest Symmetric String

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

随机推荐

  1. Spring 事务管理案例

    事务管理简介   Spring 事务管理有两种方式:一种是编程式事务管理,即通过编写代码实现事物管理,包括定义事务的开始,程序正常执行后的事物提交,异常时进行的事务回滚.另一种是基于AOP技术实现的声 ...

  2. [Erlang34]erlang.mk的源码阅读1-入门makefile

    通过erlang.mk项目,掌握基本的makefile语法,可以自己定制makefile. 1. makefile 基本规则: 1. 所有的源文件没有被编译过,则对各个源文件进行编译并进行链接,生成最 ...

  3. TSQL--如何突破PRINT的8000大限

    相信很多DBA都喜欢干的一件事就是拼SQL语句,我也不例外,但是PRINT只能打印4000的Unicode string或8000的Non-unicode string, 这个蛋疼的限制会导致过长的s ...

  4. Selenium框架切换-----Selenium快速入门(七)

    上一篇说了窗口的切换,本篇说说框架的切换. 切换框架:是指切换html中的iframe标签元素或者frame标签元素,注意,并不包括frameset 以下是常用的方法: 方法 说明 WebDriver ...

  5. 2-初步了解C#-类与对象

    本篇博客对应视频讲解 回顾 我们在第一篇文章中讲了编程中最基本的内容,如输入输出.字符串处理.数字类型计算.分支及循环结构等.无论学习什么语言,这些内容都是相通的. 本篇博客主要演示列表(List)的 ...

  6. PHP header函数设置http报文头示例详解以及解决http返回头中content-length与Transfer-Encoding: chunked的问题

    最近在服务器上,多媒体与设备(摄像头)对接的时候,总是发生错误导致设备崩溃,抓包发现响应头不对,没有返回length,使得摄像头立即崩溃.找了一下资料,改了一下响应头就好了. //定义编码 heade ...

  7. ping使用

    while read line do ip=`echo $line | awk '{print $2}' ` -i $ip ];then echo $line | tee -a b fi

  8. 【OCP-12c】2019年CUUG OCP 071考试题库(75题)

    75.Which statements are correct regarding indexes? (Choose all that apply.) A. A non-deferrable PRIM ...

  9. loadrunner录制的基本知识

    1.http/html录制选择web_url,如下图所示: 开始录制->Options->Recording->HTML Advanced->选择web_url->OK ...

  10. Nginx + uWSGI 配置django---终极版

    好开森,配置了差不多一天的项目,终于成功了,写一篇博客庆祝一下 我们先来了解下nginx与uwsgi的概念,再去配置 磨刀不误砍柴工. nginx 是一个开源的高性能的 HTTP 服务器和反向代理:1 ...