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][难]的更多相关文章

  1. PAT 1040 Longest Symmetric String

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

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

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

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

  4. 1040. Longest Symmetric String (25)

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

  5. 1040 Longest Symmetric String (25分)(dp)

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

  6. PAT 甲级 1040 Longest Symmetric String

    https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...

  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 (Advanced Level) 1040. Longest Symmetric String (25)

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

随机推荐

  1. 【抓包分析】 charles + 网易mumu 模拟器数据包

    charles  的使用.我就不再多说了.可以参考以往文章,传送门: https://www.cnblogs.com/richerdyoung/p/8616674.html 此处主要说网易模拟器的使用 ...

  2. [转]C++中模板的特化与偏特化

    转载自:http://hi.baidu.com/klcdyx2008/blog/item/5adbf77b79f316f90bd1873c.html 1.引言C++中的模板分为类模板和函数模板,虽然它 ...

  3. 【Spring Boot&&Spring Cloud系列】提高数据库访问性能

    前言 使用关系型数据库的应用系统的性能瓶颈最终还是数据库.随着业务的迅速增长,数据量会不断增大,会逐渐暴露关系型数据库的弱点,性能会大幅度的降低 项目地址:https://github.com/And ...

  4. jQuery队列(三)

    看了一下队列剩下的几个方法,在没有应用场景的情况下,对它所做的一些处理不能明白.后续希望可以通过动画部分代码的阅读能搞清楚这些处理的意义.jQuery.fn.extend({ // 推迟队列中函数的执 ...

  5. Eclipse的控制台console经常闪现

    Eclipse的控制台console有时候经常闪现!  让它不经常的调出来,可以按下面的操作去掉它: windows  ->   preferences   ->  run/debug   ...

  6. CSS学习之定位

    CSS相对定位        设置为相对定位(relative)的元素会偏移某个距离,元素仍保持其未定位前的形状,他原本所占的空间仍然保留 相对定位是一个非常容易掌握的概念,如果对一个元素进行相对定位 ...

  7. Unity3D笔记 英保通十 射线碰撞器检测

    射线碰撞检测可以用来检测方向和距离: 通过Physics.RayCast光线投射来实现:常用于射击利用发射的射线来判断.还有对战中刀剑交战中.. 一.要涉及到RayCast和RayCastHit 1. ...

  8. js之数据类型及类型转换

    一.数据类型 js中的数据类型:      5种基础类型:Undefined,Null,Boolean,Number,String      1种复合类型:Object(对象包括数组,函数等)   1 ...

  9. MySQL在linux上(cmake)的source code安装方法

    1.安装前准备: 1)必备的包和工具  gcc/g++ :MySQL 5.6开始,需要使用g++进行编译.  cmake  :MySQL 5.5开始,使用cmake进行工程管理,cmake需要2.8以 ...

  10. 数据挖掘领域十大经典算法之—C4.5算法(超详细附代码)

    https://blog.csdn.net/fuqiuai/article/details/79456971 相关文章: 数据挖掘领域十大经典算法之—K-Means算法(超详细附代码)        ...