PAT甲级——A1040 Longest Symmetric String
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
#include <iostream>
#include <string>
#include <algorithm> using namespace std;
string str, t1, t2;
int res = ;
//最普通的遍历
void way1()
{
for (int i = ; i < str.length(); ++i)
{
for (int j = str.length() - ; j > i; --j)
{
t1.assign(str.begin() + i, str.begin() + j + );
t2.assign(t1.rbegin(), t1.rend());
if (t1 == t2)
res = res > t1.length() ? res : t1.length();
}
}
} //利用回文子串中心的两边相同
void way2()
{
for (int i = ; i < str.size(); ++i) {
int j;
for (j = ; i - j >= && i + j < str.size() && str[i + j] == str[i - j]; ++j);//以当前字符为回文中心查找最长回文子串
res= max(res, * j - );//更新回文子串最大长度
for (j = ; i - j >= && i + j + < str.size() && str[i - j] == str[i + + j]; ++j);//以当前字符为回文中心左侧字符查找最长回文子串
res = max(res, * j);//更新回文子串最大长度
}
} //使用动态规划
void way3()
{
int dp[][];
for (int i = ; i < str.length(); i++)
{
dp[i][i] = ;
if (i < str.length() - && str[i] == str[i + ])
{
dp[i][i + ] = ;
res = ;
}
}
for (int L = ; L <= str.length(); L++) {
for (int i = ; i + L - < str.length(); i++) {
int j = i + L - ;
if (str[i] == str[j] && dp[i + ][j - ] == ) {
dp[i][j] = ;
res = L;
}
}
}
} int main()
{
getline(cin, str);
way1();
cout << res << endl;
return ;
}
PAT甲级——A1040 Longest Symmetric String的更多相关文章
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- PAT 甲级 1040 Longest Symmetric String
https://pintia.cn/problem-sets/994805342720868352/problems/994805446102073344 Given a string, you ar ...
- 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[dp][难]
1040 Longest Symmetric String (25)(25 分) Given a string, you are supposed to output the length of th ...
- 1040. Longest Symmetric String (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1040 题目: 1040. Longest Symmetric String (25) 时间限制 ...
- PAT1040:Longest Symmetric String
1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- 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 ...
- pat1040. Longest Symmetric String (25)
1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
- 1040 Longest Symmetric String (25分)(dp)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...
随机推荐
- iserver中的服务数据迁移
今天需要将iserver测试服务器上的空间数据服务(数据源是Oracle Plus)迁移到客户的正式服务器,原想需要很大的工作量,其实是这样简单: 一.保证客户的iserver环境都已安装正确.对于o ...
- System.Web.Mvc.RouteAttribute.cs
ylbtech-System.Web.Mvc.RouteAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Pub ...
- 委托_deleget
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- VIM 代码自动补全, YouCompleteMe安装及配置
效果 下载 使用Vundle安装 YCM 1. 安装Vundle window用户安装vundle参考这里:Windows下 vundle的安装和使用 2.
- 三种方法实现MNIST 手写数字识别
MNIST数据集下载: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist ...
- NEO4J 图数据库使用APOC数据导入
Neo4j 数据导入 一.安装与部署 直接在官网下载安装包安装,解压即可. 二.下载相应的jar包 apoc 包下载链接: https://github.com/neo4j-contrib/ne ...
- Win10系统无法安装可选功能提示错误代码0x800F081F的解决方法
DISM /Online /Cleanup-Image /RestoreHealth /Source:wim:H:\sources\install.wim:1 /limitaccess
- 【核心核心】10.Spring事务管理【TX】XML+注解方式
转账案例环境搭建 1.引入JAR包 IOC的6个包 AOP的4个包 C3P0的1个包 MySQL的1个驱动包 JDBC的2个目标包 整合JUnit测试1个包 2.引入配置文件 log4j.proper ...
- 在自己的工程中使用开源界面库Duilib
配置duilib库 一个简单的使用Duilib程序一般要在stdafx.h中进行配置(链接duilib的文件,包括头文件).通常的配置代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 ...
- golang的表格驱动测试
一.leetcode的算法题 package main import ( "fmt" "strings" ) func lengthOfNonRepeating ...