PAT1040:Longest Symmetric String
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 思路 求一个字符串的最长回文子串。 DP的思想,复杂度O(n^2)。
1.如果一个从i到j的字符串str(i,j)的子串str(i+1,j-1)为回文串,那么在str[i] == str[j]的情况下,字符串str(i,j)也是回文串。
2.每一个字符本身就是一个回文串。所以在每一个字符的基础上,根据1的条件来确定更长的回文串。
3.用一个bool数组isSym[1001][1001]来列举所有的情况,isSym[i][j]表示起始位置为i、终止位置为j的字符串是否是回文串。
4.检查是否有N个长度的回文串,更新最大长度maxlength。(1 <=N <= str.length())。 代码
#include<iostream>
#include<vector>
using namespace std;
vector<vector<bool>> isSym(1001,vector<bool>(1001,false));
int main()
{
string s;
getline(cin,s);
int maxlength = 1;
const int Length = s.size();
for(int i = 0;i < Length;i++)
{
isSym[i][i] = true;
if(i < Length - 1 && s[i] == s[i + 1])
{
isSym[i][i+1] = true;
maxlength = 2;
}
} for(int len = 3;len <= Length;len++)
{
for(int i = 0;i <= Length - len;i++)
{
int j = i + len - 1;
if(isSym[i+1][j-1] && s[i] == s[j])
{
isSym[i][j] = true;
maxlength = len;
}
}
}
cout << maxlength << endl;
}
PAT1040:Longest Symmetric String的更多相关文章
- pat1040. Longest Symmetric String (25)
1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- PAT1040 Longest Symmetric String (25分) 中心扩展法+动态规划
题目 Given a string, you are supposed to output the length of the longest symmetric sub-string. For ex ...
- 1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...
- PAT 1040 Longest Symmetric String[dp][难]
1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...
- 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 ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- A1040. Longest Symmetric String
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
- PAT 甲级 1040 Longest Symmetric String
https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...
- PAT甲级——A1040 Longest Symmetric String
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
随机推荐
- Cocos2D遍历场景图(Scene Graph)
另一个Cocos2D有用的调试特性是打印出递归的打印出节点的孩子们. 你可以添加以下一行到MainScene或GameScene的didLoadFromCCB的方法中: [self.scene wal ...
- CSS中让一个div的高度随着另外个一个统计的div的高度变化而变化的代码
.w1002 .left_part{overflow:hidden; padding-bottom:9999px; margin-bottom:-9999px;display:inline;}
- Android NDK开发method GetStringUTFChars’could not be resolved
Android NDK开发method GetStringUTFChars'could not be resolved 图1 最近用到android的ndk,但在eclipse中提示method Ge ...
- android 向webview传值
android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用. 效果图: (一)Android部分: 布局代码: <spa ...
- linux命令大全(自己慢慢看)
http://blog.zol.com.cn/874/article_873769.html rm -rf mydir /* 删除mydir目录 */ cd mydir /* 进入mydir目录 */ ...
- LeetCode之“字符串”:ZigZag Conversion
题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- 分别修改Cube每个面的贴图UV(Unity3D开发之十八)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...
- kettle 的表输出 table output
kettle的表输出: 双击后,看设置, 1,在connecttion后面,点击new里新建一个.设定各个选项值,如选择mysql类型,则配置hostname,database name,端口, 用户 ...
- win7待机时间设置,睡眠时间设置
首先,单击"开始"找到"控制面板" 步骤阅读 2 打开"控制面板"以后,单击右侧的"硬件和声音" 步骤阅读 3 找到&q ...
- 演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史
演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史 原文出处: 少数派 9 月 18 日苹果就将推出 iOS 8 正式版了,从 2007 年发布第一代 iPhone 时搭载在 iPh ...