pta l2-8(最长对称字串)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376
题意:求给定字符串的最长回文串的长度。
思路:数据太弱了,暴力就行了,遍历0到len-1,当前点为i,则以i为中点的奇回文串是从i-j到i+j(1<=j<=len),偶回文串从i-j+1到i+j,两层循环。然后吐槽一下pta的题,题目说了输入字符串为非空,但有个测试点就卡了孔字符串,需要输出0,写天梯赛的题还是多注重细节,尤其是边界条件。
AC代码:
#include<bits/stdc++.h>
using namespace std; string s;
int len,res,tmp; int main(){
getline(cin,s);
len=s.length();
for(int i=;i<len;++i){
tmp=;
for(int j=;j<len;++j){
if(i-j<||i+j>=len||s[i-j]!=s[i+j])
break;
tmp+=;
}
if(tmp>res) res=tmp;
tmp=;
for(int j=;j<len;++j){
if(i-j+<||i+j>=len||s[i-j+]!=s[i+j])
break;
tmp+=;
}
if(tmp>res) res=tmp;
}
printf("%d\n",res);
return ;
}
pta l2-8(最长对称字串)的更多相关文章
- PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)
1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the ...
- URAL 1517 Freedom of Choice(后缀数组,最长公共字串)
题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- 最长公共子序列(LCS)问题 Longest Common Subsequence 与最长公告字串 longest common substr
问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk ...
- 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14113 Accepted: 6260 Descr ...
- HDU 1423 最长公共字串+上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...
- 最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...
随机推荐
- spark 常用技巧总结2
zip拉链操作 def zip[U](other: org.apache.spark.rdd.RDD[U])(implicit evidence$10: scala.reflect.ClassTag[ ...
- 3. orcle导入导出dmp文件并更改表空间
0.数据泵导入导出: expdp test/test@10.0.0.11/orcl schemas=test dumpfile=test.dmp directory=DPDATA logfile= ...
- Notification html5 的通知api
https://developer.mozilla.org/zh-CN/docs/Web/API/notification 使用方法 var notification = new Notificati ...
- J2SE 8的流库 --- 基本类型流的使用
展现流的方法 public static <T> void show(String title, Stream<T> stream){ System.out.println(& ...
- Xcode 8 注释快捷键失效
sudo /usr/libexec/xpccachectl 重启
- 【转】解决Eclipse中SVN版本信息不显示的问题
eclipse 中使用 svn 插件,原本正常,未作任何更改,最近几天突然eclipse 中查看文件时,文件后面的 版本号 . 文件的状态图标 等等都不见了.以为有插件冲突,卸载了好多其他的相关的插件 ...
- postmessage/cors跨域postMessage、xhr2和xmldomain
一.h5 postMessage node http-server配置服务器 有关配置:请参考我的http://www.cnblogs.com/leee/p/5502727.html 我把文件夹a配置 ...
- 16.2 profile 显示或者隐藏页面 修改密码
我们auth在clent端有更加强大的功能 显示或者隐藏component 或者 我们可以阻止或者允许某个用户访问url
- 解决在.NET 4.0下无法发送包含尖括号等请求的问题
今天在做一个简单的数据添加时,使用jQuery的ajax的post操作,发现如果包含尖括号等html格式数据的请求无法发给服务端. 以往的做法就是在页面中或web.config中的pages节点中增加 ...
- 简单自定义UIToolBar
let item1 = UIBarButtonItem(title: "分享", style: .Plain, target: self, action: nil) let ite ...