【面试笔试算法】Problem 9: 腾讯2016年研发实习笔试题:最长回文子串
(一)题目
问题:求给定字符串s的回文(palindrome)子串中,长度最大的回文子串的长度。
回文(palindrome)是指从左往右读和从右往左读字符串,看到的字符串都是一样的。比如“cabbeaf”,回文子串包括”c”“aba”“abba”等,最长的子串是“abba”,长度为4.
程序输入:“cabbeaf”
程序输出:4
(二)解题
当时笔试的时候没有AC,线下在VS上调出来了。
此方法的主要思想是:动态规划,利用一个path[i][j]数组记录字符串i到j的最长回文长度,状态转移方程分以下两种情况:
1、如果s[i]==s[j],则回文串长度为path(i+1,j-1)+2;
2、如果s[i]!=s[j],则取max(path(i+1,j),path(i,j-1));
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int path[1000][1000] ={0};
int maxlen=0;
int findPalindrome(string& s,int start , int end)
{
if (start > end)
return 0;
if (start == end)
return 1;
if (path[start][end] != 0)
return path[start][end];
if (s[start] == s[end])
{
path[start][end] = findPalindrome(s,start+1,end-1) +2;
if (maxlen<path[start][end])
{
maxlen = path[start][end];
}
}
if (s[start] != s[end])
{
int temp1 =findPalindrome(s,start+1,end);
int temp2 =findPalindrome(s,start,end-1);
path[start][end] = temp1>temp2?temp1:temp2;
if (maxlen<path[start][end])
{
maxlen = path[start][end];
}
}
return maxlen;
}
int main (){
string s = "cabbeaf";
findPalindrome(s,0 ,s.length()-1);
cout<<maxlen<<endl;
return 0;
}
没有在OJ平台上测试,也不知道能不能AC,反正大致思想是这样的。
【面试笔试算法】Problem 9: 腾讯2016年研发实习笔试题:最长回文子串的更多相关文章
- Manacher 求最长回文子串算法
Manacher算法,是由一个叫Manacher的人在1975年发明的,可以在$O(n)$的时间复杂度里求出一个字符串中的最长回文子串. 例如这两个回文串“level”.“noon”,Manacher ...
- 面试经典算法:马拉松算法,最长回文子串Golang实现
求一个字符串中最长的回文子串. package main import "fmt" /* 马拉松算法,求最长回文子串,时间复杂度:线性 */ func main() { // 回文 ...
- 九度OJ 1528 最长回文子串 -- Manacher算法
题目地址:http://ac.jobdu.com/problem.php?pid=1528 题目描述: 回文串就是一个正读和反读都一样的字符串,比如"level"或者"n ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
- hihocoder #1032 : 最长回文子串 Manacher算法
题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...
- 51Nod 1089 最长回文子串 V2 —— Manacher算法
题目链接:https://vjudge.net/problem/51Nod-1089 1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: ...
- hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]
传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...
- hdu5371 最长回文子串变形(Manacher算法)
pid=5371">http://acm.hdu.edu.cn/showproblem.php? pid=5371 Problem Description Hotaru Ichijou ...
- 使用manacher算法解决最长回文子串问题
要解决的问题 求一个字符串最长回文子串是什么.且时间复杂度 O(N) 具体描述可参考: LeetCode_5_最长回文子串 LintCode_200_最长回文子串 暴力解法 以每个字符为中心向左右两边 ...
随机推荐
- ubuntu安装qq
安装的版本是国际版 1.安装依赖库 sudo apt-get install libgtk2.0-0:i386 sudo apt-get install lib32ncurses5 2.下载 下载链接 ...
- 分布式内存文件系统Tachyon
UCBerkeley研发的Tachyon(超光子['tækiːˌɒn],名字要不要这么太嚣张啊:)是一款为各种集群并发计算框架提供内存数据管理的平台,也可以说是一种内存式的文件系统吧.如下图,它就处于 ...
- 闪屏页面开发遇到的问题you need to use a theme.appcompat theme (or descendant)
开始做一个新闻客户端的应用,在做到闪屏页面时之前发布应用的时候总是报错,原因是我在splash.java中把Activty写成ActionBarActivity,导包,然后就可以了.以前也遇到过这种情 ...
- linux中echo的用法
1.echo命令我们常用的选项有两个,一个是-n,表示输出之后不换行,另外一个是-e,表示对于转义字符按相应的方式处理,如果不加-e那么对于转义字符会按普通字符处理. 2.echo输出时的转义字符 \ ...
- JS滚动显示
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- ROS_Kinetic_21 使用Qt Creator Plug in即ros_qtc_plugin
更为详细版本请参考: http://blog.csdn.net/zhangrelay/article/details/52214411 结合看更为具体. 首先,先上原版参考: 1 http://wik ...
- python类:描述器Descriptors和元类MetaClasses
http://blog.csdn.net/pipisorry/article/details/50444769 描述器(Descriptors) 描述器决定了对象属性是如何被访问的.描述器的作用是定制 ...
- python读写word、excel、csv、json文件
http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...
- mysql5.6升级到5.7后Sequel Pro无法连接解决
因为装ntop,brew自动升级了本地的Mysql,结果升级完成之后,使用Sequel Pro连接总是报错,使用mysql 命令行工具就没有问题. OSX版本 10.11.5 Mysql版本 5.6 ...
- MyBatis主键生成器Jdbc3KeyGenerator(二)
上一篇博客MyBatis主键生成器KeyGenerator(一)中我们大体介绍了主键生成器的接口及配置等,接下来我们介绍一下KeyGenerator的实现类Jdbc3KeyGenerator Jdbc ...