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. 【Python】给程序加个进度条

    对于开发或者运维来说,使用 Python 去完成一些跑批任务,或者做一些监控事件是非常正常的情况.那么如何有效地监控任务的进度?除了在任务中加上 Log 外,还能不能有另一种方式来了解任务进展到哪一步 ...

  2. 二、K3 Cloud 开发插件《K3 Cloud 常用数据表整理》

    一.数据库查询常用表 --查询数据表select * from ( ),t1.FKERNELXML.query('//TableName')) as 'Item',t1.FKERNELXML,t2.F ...

  3. IIS7 windows 下安装PHP

    首先要下载 php for windows 这个软件,百度一搜一大把. 1.我安装的是5.3.22版,就以这个为例给大家解说一下. 安装就是下一步,下一步,到一个选项 IIS FastCGI 选这个! ...

  4. Spark学习笔记--Spark在Windows下的环境搭建

    本文主要是讲解Spark在Windows环境是如何搭建的 一.JDK的安装 1.1 下载JDK 首先需要安装JDK,并且将环境变量配置好,如果已经安装了的老司机可以忽略.JDK(全称是JavaTM P ...

  5. Linux "bring up eth0 failed, eth0 seems not be presernt" 问题解决方案

    =========1.问题========== 重启网卡的时候出现"bring up eth0 failed, eth0 seems not be presernt", 提示找不到 ...

  6. OPENQUERY (Transact-SQL),跨数据库操作。

    在指定的链接服务器上执行指定的传递查询. 该服务器是 OLE DB 数据源. OPENQUERY 可以在查询的 FROM 子句中引用,就好象它是一个表名.OPENQUERY 也可以作为 INSERT. ...

  7. 几个解决k染色问题的指数级做法

    几个解决k染色问题的指数级做法 ——以及CF908H题解 给你一张n个点的普通无向图,让你给每个点染上k种颜色中的一种,要求对于每条边,两个端点的颜色不能相同,问你是否存在一种可行方案,或是让你输出一 ...

  8. 8.20 前端 js

    2018-8-20 17:40:12 js参考: https://www.cnblogs.com/liwenzhou/p/8004649.html 2018-8-20 20:33:31 css学完了 ...

  9. 【vue】vue.js安装教程/vue项目搭建

    前提:已安装nodejs——npm  (备注教程  “物理安装”  ) 第一步:建了一个managerSys文件夹,用于保存项目 第二步:从cmd进入该文件夹,之后开始安装vue.js相关 1)在该项 ...

  10. php里面向指定的页面提交数据

    在jquery里用 load post 等等,无法得到我想要的结果!于是突然-----这几天想的东西都白想了,现在只好这样了 现在想在php里面向指定的页面提交数据,应该有,还可以有返回值 于是找了这 ...