L2-008 最长对称子串 (25 分) (模拟)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376
题目:
对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
思路:
遍历字符串中的每个位置 在每个位置枚举长度判断此长度能否构成一个回文串 需要对长度的奇偶性分类讨论
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
string s; int main(){
getline(cin,s);
int len=s.length();
int ans=;
for(int i=;i<len;i++){
for(int j=;i-j>= && i+j<len;j++){
if(s[i-j]!=s[i+j]) break;
ans=max(ans,j*+);
}
for(int j=;i-j>= && i+j+<len;j++){
if(s[i-j]!=s[i+j+]) break;
ans=max(ans,j*+);
}
}
printf("%d\n",ans);
return ;
}
L2-008 最长对称子串 (25 分) (模拟)的更多相关文章
- L2-008 最长对称子串 (25 分)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...
- [刷题] PTA 7-64 最长对称子串
7-64 最长对称子串 我的代码: 1 #include<stdio.h> 2 #include<string.h> 3 #define N 1001 4 5 int main ...
- 团体程序设计天梯赛-练习集L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- c语言:最长对称子串(3种解决方案)
问题描述: 输入一个字符串,输出该字符串中最大对称子串的长度.例如输入字符串:“avvbeeb”,该字符串中最长的子字符串是“beeb”,长度为4,因而输出为4. 解决方法:中序遍历 一,全遍历的方法 ...
- L2-008. 最长对称子串(思维题)*
L2-008. 最长对称子串 参考博客 #include <iostream> using namespace std; int main() { string s; getline(ci ...
- pat 团体赛练习题集 L2-008. 最长对称子串
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
- L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- PAT L2-008 最长对称子串(模拟字符串)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...
- 天梯赛L2-008 最长对称子串 (字符串处理)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
随机推荐
- [C++项目]2048控制台游戏
#include <iostream> #include <windows.h> #include <ctime> using namespace std; ; ; ...
- 一、commander
#!/usr/bin/env node const program = require('commander'); const colors = require('colors'); const pk ...
- MongoDB ODBC Driver for Data Integration with Power BI
This guide will walk you through connecting Microsoft Power BI to a MongoDB DataSet using our MongoD ...
- jqgrid的增删改查
这个是要写的页面(需要引入下面的js页面) 1 <div class="modal-body" width="100%" style="over ...
- python面试题整合
面试题整合 面试题—并发编程部分
- TCP/IP的四元组、五元组、七元组
TCP/IP的四元组.五元组.七元组 四元组是: 源IP地址.目的IP地址.源端口.目的端口 五元组是: 源IP地址.目的IP地址.协议号.源端口.目的端口 七元组是: 源IP地址.目的IP地址.协议 ...
- Magento 2 自带模态的应用
Modal widget in Magento 2 Magento 2 自带模态的应用 使用magento 2 的自带模态组件,以下代码只供参考使用. 1,DOM >模态块与触发元素 .pthm ...
- Vue(服务端渲染)
一.前言 1.服务端渲染图解 2.简介服务端渲染 ...
- linux通过expect工具来实现自动登录服务器,并执行相关操作
参考地址:https://www.cnblogs.com/liyuanhong/articles/7728034.html EOF的使用参考:https://www.cnblogs.com/liyua ...
- 深入剖析Kubernetes学习笔记:容器基础(05-06)
05 :从进程说起 1.容器本身没有价值,有价值的是"容器编排" 2.什么是进程? 一旦"程序"被执行起来,它就从磁盘上的二进制文件,变成 1.计算机内存中的数 ...