PAT 1040 Longest Symmetric String[dp][难]
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
题目大意:
//自己写的土鳖方法,以每个字符串为对称中心进行判断。一开始只考虑了对称长度是偶数的情况,没有考虑奇数。得了21分。加上判断奇数的,只得了23分,还是有一个测试点没过去,没找到是什么原因,暂时放一下。
#include <iostream>
#include <algorithm>
#include <string>
#include<stdio.h>
using namespace std; int main() {
string s="";
char ch;
while(ch=getchar()){
if(ch=='\n')break;
if(ch==' ')s+=" ";
else s+=ch;
}
int len=s.size();
int ct=,tp=;
for(int i=;i<len-;i++){
for(int j=;j<=i;j++){
if(i+j>=len)break;
if(s[i-j]==s[i+j])
tp++;
else break;
}
if(*tp+>ct)ct=*tp+;
tp=;
}
tp=;
for(int i=;i<len-;i++){
for(int j=;j<=i;j++){
if(i+j+>=len)break;
if(s[i-j]==s[i+j+])
tp++;
else break;
}
if(*tp>ct)ct=*tp;
tp=;
}
cout<<ct;
return ;
}
这个代码也是通过判断对阵中心,不过使用了reverse函数, 以前见过的,对string判断对称,使用reverse
通过截取,如果是偶数时,那么对称中心就是中间靠左的那个,(和中位数一样。)
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string s;
getline(cin, s);
int len = ;
for (int i = ; i < s.size(); i++){
for (int j = ; j <= min(i, int(s.size())--i); j++){
//这个最小值表示向左向右还最多可以截取几个。是需要min来限制的。
string s2 = s.substr(i-j,*j+);
string s3 = s2;
reverse(s2.begin(), s2.end());
if (s2 == s3){
if (s2.size() > len)
len = s2.size();
}
string s4 = s.substr(i-j, *j+);//这个如果i指向,最后一个,是会和上边重判的。是截取一个。
string s5 = s4;
reverse(s4.begin(), s4.end());
if (s4 == s5){
if (s4.size() > len)
len = s4.size();
}
}
}
printf("%d\n", len);
return ;
}
//下面是大佬的dp版本:https://www.liuchuo.net/archives/2104
#include <iostream>
#include<stdio.h>
using namespace std;
int dp[][];//dp[i][j]只有0和1取值,表示i和j之间是否是
int main() {
string s;
getline(cin, s);//直接getline可以读进去字符串里。
int len = s.length(), ans = ;
for(int i = ; i < len; i++) {
dp[i][i] = ;
if(i < len - && s[i] == s[i+]) {
dp[i][i+] = ;
ans = ;
}
}
for(int L = ; L <= len; L++) {
for(int i = ; i + L - < len; i++) {//总长度的限制。
int j = i + L -;
if(s[i] == s[j] && dp[i+][j-] == ) {
dp[i][j] = ;
ans = L;
}
}
}
printf("%d", ans);
return ;
}
//dp数据只有0和1取值,dp[i][j]表示i到j是否是对称的,为了保证状态的转移,使用长度作为循环,因为2很好判断,那么就从L=3开始,i每次都从0开始,那么j就是那个对应的结束,要满足的条件自然是j+L-1<len了。而最终的答案自然是最大的L。还有dp[i+1][j-1]也是神了,这就是坐进右退判断对称的。
PAT 1040 Longest Symmetric String[dp][难]的更多相关文章
- PAT 1040 Longest Symmetric String
#include <cstdio> #include <cstdlib> using namespace std; ]; ]; int syslen(char str[], i ...
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- 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 ...
- 1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...
- 1040 Longest Symmetric String (25分)(dp)
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 ...
- 1040 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 (25)-求最长回文子串
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789177.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT (Advanced Level) 1040. Longest Symmetric String (25)
暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ]; ...
随机推荐
- C语言预处理命令详解
一 前言 预处理(或称预编译)是指在进行编译的第一遍扫描(词法扫描和语法分析)之前所作的工作.预处理指令指示在程序正式编译前就由编译器进行的操作,可放在程序中任何位置. 预处理是C语言的一个重要功能 ...
- 【laravel5.6】laravel 自定义公共函数
1.在 app/Helpers/ 新建一个文件 functions.php,当然这个文件位置和名称你可以自己定义,创建一些函数用于全局调用: 2.在composer.json中的autoload下增加 ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot项目集成Swagger UI
前言 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
- 正则表达式取querystring
var s = decodeURIComponent((new RegExp('[?|&]userid=([^&;]+?)(&|#|;|$)').exec(location.h ...
- aws.s3的 upload 和putObject有什么区别
相同点:上传或新增一个object : <template> <div class="page"> <!-- 参考:https://blog.csdn ...
- sencha touch list(列表) item(单行)单击事件触发顺序
测试代码如下 Ext.define('app.view.new.List', { alternateClassName: 'newList', extend: 'app.view.util.MyLis ...
- 下载Google Play外国区APP技巧
安卓用户若遇到喜欢的APP是外国区的,只要FQ就能下载.比起果粉还要注册,是简便很多.但有没有更简单的办法?这个必须有!笔者前几天在网上闲逛时,就发现了一个给力的网站.让你不用FQ,只需3个步骤,就能 ...
- async 与await
一. async 与await (https://segmentfault.com/a/1190000007535316) 1.async 是“异步”的简写,而 await 可以认为是 async w ...
- FileInputStream 和 FileOutputStream
简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...
- AutoMapper queryable extensions 只找需要的字段
http://jahav.com/blog/automapper-queryable-extensions/ How to generate a LINQ query for your DTOs Au ...