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. 输入格式: 输入在一 ...
随机推荐
- Ajax的post方式提交数据
最新需要学习如何使用 POST 提交方法的接口,正好看到了Ajax 版本的感觉不错分享给大家,欢迎高手指点. <SCRIPT LANGUAGE=”javascript”> <!– f ...
- Codeforces Gym101606 A.Alien Sunset (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))
2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017) 寒假第一次组队训练赛,和学长一起训练,题目难度是3颗星,我和猪队友写 ...
- ASP.NET HttpContext类
IHttpHandler 接口 定义 ASP.NET 以异步方式处理使用自定义 HTTP 处理程序的 HTTP Web 请求而实现的协定 封装http请求信息 HttpContext.Curren ...
- Codeforces 622C Not Equal on a Segment 【线段树 Or DP】
题目链接: http://codeforces.com/problemset/problem/622/C 题意: 给定序列,若干查询,每个查询给定区间和t,输出区间内任意一个不等于t的元素的位置. 分 ...
- Sping、SpringMVC、SpringBoot的对比
原文链接:https://dzone.com/articles/spring-boot-vs-spring-mvc-vs-spring-how-do-they-compare 作者: Ranga Ka ...
- A Good User Interface
has high conversion rates and is easy to use. In other words, it's nice to both the business side as ...
- PopupMenu和对话框的使用
PopupMenu和对话框的使用 1.菜单的使用之 PopupMenu 步骤:1.创建popupMenu对象 参数:上下文对象 当前view对象 2.利用getMenu().addSubMenu创 ...
- json解析神器 jsonpath的使用
转载:http://blog.csdn.net/qq_20641565/article/details/77162868 如果项目需求是从某些复杂的json里面取值进行计算,用jsonpath+IK( ...
- Triangle 三角形——找出三角形中从上至下和最小的路
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- TI C66x DSP 四种内存保护问题 -之- 针对CPU訪问外存(DDR3 or MSM)时的内存保护问题 - 举例
在代码维护中遇到过这种问题,CPU訪问了corePac的外部内存空间0x75510C55地址,即CPU向corePac的L2内存控制器发起了对该内存的訪问,然后L2内存控制器将该请求发给corePac ...