PAT 天梯赛 L2-008. 最长对称子串 【字符串】
题目链接
https://www.patest.cn/contests/gplt/L2-008
思路
有两种思路
第一种
遍历每一个字符
然后对于每一个 字符
同时 往左 和 往右 遍历 只要 此时 左右两边所指的字符 相同 就可以继续往下遍历 然后更新答案
但是这种情况
要分 奇数回文 和 偶数回文
有些麻烦
所以 我们能不能 转换成 一种情况
将字符串 格式化 一下就可以了
比如
moom 这个 字符串
在首尾和字符间 加上一个 不会出现的字符 比如 ‘#’
格式化成
“#m#o#o#m#”
这样
然后 就变成了 奇数 回文
就只有一种情况了 就是O(n^2) 的时间复杂度
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7;
int main()
{
string s;
getline(cin, s);
string cnt = "#";
int len = s.size();
for (int i = 0; i < len; i++)
cnt += s[i], cnt += '#';
len = cnt.size();
int ans = 1;
for (int i = 0; i < len; i++)
{
int temp = 0;
if (cnt[i] != '#')
temp ++;
for (int j = i - 1, l = i + 1; j >= 0 && l < len; j--, l++)
{
if (cnt[j] == cnt[l])
{
if (cnt[j] != '#')
temp += 2;
}
else
break;
}
ans = max(ans, temp);
}
printf("%d\n", ans);
}
PAT 天梯赛 L2-008. 最长对称子串 【字符串】的更多相关文章
- pat 团体赛练习题集 L2-008. 最长对称子串
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
- PAT天梯赛L2-008 最长对称字符串
题目链接:点击打开链接 对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&a ...
- 团体程序设计天梯赛-练习集L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- 天梯赛L2-008 最长对称子串 (字符串处理)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...
- PAT L2-008 最长对称子串(模拟字符串)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...
- 天梯杯 L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- PTA天梯赛L2
L2-001 紧急救援 题意:就是给你一张n<500的图:让你求最短路径,最短路条数,以及路径: 做法,先用dijkstra求最短路,然后dfs找最短路条数,以及点权的最大值: 一般dfs不就可 ...
- L2-008 最长对称子串 (25 分) (模拟)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376 题目: 对给定的字符串,本题要求你输出 ...
- L2-008. 最长对称子串
L2-008. 最长对称子串 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对给定的字符串,本题要求你输出最长对称子串的长度. ...
- L2-008 最长对称子串 (25 分)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11. 输入格式: 输入在一 ...
随机推荐
- git commit或pull后恢复到原来版本
https://blog.csdn.net/litao31415/article/details/87713712
- va_list 简介
原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...
- Chelly的串串专题
CF149E 题意:给出一个长度为n的文本串和m个模式串,求有多少个模式串可以拆成两半,使得这两半按顺序匹配(n<=2e5,m<=100) 最暴力的想法就是对于每个询问串,全部和原串做一遍 ...
- Java创建和解析Json数据方法(五)——Google Gson包的使用
(五)Google Gson包的使用 1.简介 Gson包中,使用最多的是Gson类的toJson()和fromJson()方法: ①toJson():将java对象转化为json数据 ...
- 微信小程序 压缩图片并上传
转自https://segmentfault.com/q/1010000012507519 wxml写入 <view bindtap='uploadImg'>上传</view> ...
- mysql 源码 jin-yang.github.io
https://jin-yang.github.io/post/mysql-group-commit.html
- hdoj 4828 卡特兰数取模
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...
- react 监听 移动端 手机键盘 enter 事件
处理方法: (1)html书写 form标签中去掉action参数,定义onSubmit方法,如下所示: /** * 搜索框 组件 */ import React,{PureComponent} fr ...
- 关于 AlphaGo 论文的阅读笔记
这是Deepmind 公司在2016年1月28日Nature 杂志发表论文 <Mastering the game of Go with deep neural networks and tre ...
- jdk的动态代理源代码解析
先看一下JDK的动态是怎么用的. package dynamic.proxy; import java.lang.reflect.InvocationHandler; import java.lang ...