hiho #1032: 最长回文子串
#1032 : 最长回文子串
描述
小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。
这一天,他们遇到了一连串的字符串,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能分别在这些字符串中找到它们每一个的最长回文子串呢?”
小Ho奇怪的问道:“什么叫做最长回文子串呢?”
小Hi回答道:“一个字符串中连续的一段就是这个字符串的子串,而回文串指的是12421这种从前往后读和从后往前读一模一样的字符串,所以最长回文子串的意思就是这个字符串中最长的身为回文串的子串啦~”
小Ho道:“原来如此!那么我该怎么得到这些字符串呢?我又应该怎么告诉你我所计算出的最长回文子串呢?
小Hi笑着说道:“这个很容易啦,你只需要写一个程序,先从标准输入读取一个整数N(N<=30),代表我给你的字符串的个数,然后接下来的就是我要给你的那N个字符串(字符串长度<=10^6)啦。而你要告诉我你的答案的话,只要将你计算出的最长回文子串的长度按照我给你的顺序依次输出到标准输出就可以了!你看这就是一个例子。”
- 样例输入
-
3
abababa
aaaabaa
acacdas - 样例输出
-
7
5
3
思路:
1.O(n平方)的方法
记录每个位置,从当前位置,向头尾遍历,直到左右不想等,结束,记录回文串的长度。注意:需要分奇回文和偶回文。
反正hiho上是超时了,看来必须要写一种O(n)复杂度的方法呀。
程序:
#include "iostream"
#include "string.h"
#include "string" using namespace std; string str;
int n, maxnum; int main()
{
cin >> n;
while (n--)
{
cin >> str;
maxnum = ;
for (int i = ; i < strlen(str.c_str()); i++)
{
int j = ;//ji
while (i - j >= && i + j<strlen(str.c_str()))
{
if (str[i - j] == str[i + j])
j++;
else
break;
}
j = j * - ; int k = ;//ou
while (i - k >= && i + k + <strlen(str.c_str()))
{
if (str[i - k] == str[i + k + ])
k++;
else
break;
}
k = k * ; int tag = j > k ? j : k;
if (tag > maxnum)
maxnum = tag;
}
cout << maxnum << endl;
}
}
2.
Manacher算法。时间O(n),空间O(n)。------完全OK!
主要目的就是要减少计算量,在”中心扩展“法的基础上,节省更多的计算量。下面介绍这种处理方法。
在字符串中间插入符号,将串构造成基回文串
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std; int longestPalindrome(string s) {
string ss = "#";
for (int i = ; i<s.length(); i++)
{
ss += s[i];
ss += '#';
}
vector<int> P(ss.length(), ); int center = , boundary = , maxLen = , resCenter = ;
for (int i = ; i<ss.length() - ; i++) {
int iMirror = * center - i;
P[i] = (boundary>i) ? min(boundary - i, P[iMirror]) : ;
while (i - - P[i] >= && i + + P[i] <= ss.length() - && ss[i + + P[i]] == ss[i - - P[i]])
P[i]++;
if (i + P[i]>boundary) { // 更新边界
center = i;
boundary = i + P[i];
}
if (P[i]>maxLen) { // 更新res
maxLen = P[i];
resCenter = i;
}
}
return maxLen;
} int main()
{
int n;
cin >> n; while (n--)
{
string str;
cin >> str;
cout << longestPalindrome(str) << endl;
}
return ;
}
hiho #1032: 最长回文子串的更多相关文章
- HiHo 1032 最长回文子串 (Manacher算法求解)
/** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...
- hihocoder #1032 : 最长回文子串【 manacher算法实现 】
#1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...
- hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]
传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...
- hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)
题意:给一个字符串,求最长回文子串的长度. 思路: (1)暴力穷举.O(n^3) -----绝对不行. 穷举所有可能的出现子串O(n^2),再判断是否回文O(n).就是O(n*n*n)了. (2)记录 ...
- hihocoder #1032 : 最长回文子串 Manacher算法
题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...
- hihocode #1032 : 最长回文子串【manacher】模板题
题目链接:https://vjudge.net/problem/HihoCoder-1032 manacher算法详解:https://blog.csdn.net/dyx404514/article/ ...
- [hihoCoder] #1032 : 最长回文子串
时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...
- hihocoder 1032 最长回文子串(Manacher)
传送门 #include<queue> #include<cmath> #include<cstdio> #include<cstring> #incl ...
- 【hiho一下】第一周 最长回文子串
题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...
随机推荐
- pypy3.6的下载地址和安装第三方依赖
1.不同版本的下载链接 建议使用此链接:https://bitbucket.org/pypy/pypy/downloads/ 官网的:http://doc.pypy.org/en/latest/rel ...
- 【VS开发】【电子电路技术】PCI与PCIE主板插卡与插槽识别
一.PCI PCI接口分为32bit和64bit两种,32bit就是一般台式机使用的普通的pci接口(图一.图三),64bit接口比32bit接口长一些一般只出现在服务器上(图四.图五).32bit和 ...
- Flume入门案例
首先需要通过一个配置文件来配置Agent. 通过flume提供的工具启动agent就可以工作了. (1)编写配置文件 #example.conf:单节点Flume配置 #命名Agent a1的组件 ...
- 【数据库】Redis/MongoDB/MySQL/Oracle随笔索引
数据库体系 [思维导图]数据库体系 密码: a8ni Redis JPA
- 【Linux 网络编程】常用TCP/IP网络编程函数
(1)函数socket /**************************************************************** ** 功能:创建一个套接字用于通信 ** 参 ...
- Random Pick with Weight
Given an array w of positive integers, where w[i] describes the weight of index i, write a function ...
- [转帖]IBM收购红帽价格是多少?是否会形成垄断企业?会存在什么不安因素?
http://www.techweb.com.cn/it/2019-07-10/2743776.shtml 国产的linux 用centos源的 如何是好呢.. 蓝色巨人IBM官方宣布,已经正式完成对 ...
- 查找担保圈-step4-提取s1中担保圈路径中的成员
USE [test] GO /****** Object: StoredProcedure [dbo].[p02_get_group_member] Script Date: 2019/7/8 14: ...
- Eclipse 新建.jsp页面后,页面头部标签报错的解决方法
Eclipse 新建.jsp页面后,页面头部标签报错的解决方法 1.报错地方: 2.解决方法: .jsp页面右键==>BUild Path ==>Configure Build Path. ...
- maven中scope属性的
Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * c ...