九度OJ 1528 最长回文子串 -- Manacher算法
题目地址:http://ac.jobdu.com/problem.php?pid=1528
- 题目描述:
-
回文串就是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。
回文子串,顾名思义,即字符串中满足回文性质的子串。
给出一个只由小写英文字符a,b,c...x,y,z组成的字符串,请输出其中最长的回文子串的长度。
- 输入:
-
输入包含多个测试用例,每组测试用例输入一行由小写英文字符a,b,c...x,y,z组成的字符串,字符串的长度不大于200000。
- 输出:
-
对于每组测试用例,输出一个整数,表示该组测试用例的字符串中所包含的的最长回文子串的长度。
- 样例输入:
-
abab
bbbb
abba
- 样例输出:
-
3
4
4
自己比较野蛮的方法
#include <stdio.h>
#include <string.h> int LenOfMaxPalindrome(char str[], int len, int low, int high){
int sum = 0;
while (low >= 0 && high < len){
if (str[low] == str[high]){
sum += 2;
--low;
++high;
}
else
break;
}
return sum;
} int MaxPalindrome (char str[]){
int len = strlen (str);
int mid;
int len1;
int len2;
int max; if ((len >= 2) && (str[0] == str[1]))
max = 2;
else
max = 1;
for (mid=1; mid<len-1; ++mid){
len1 = len2 = 0;
len1 = LenOfMaxPalindrome(str, len, mid - 1, mid + 1) + 1;
if (str[mid] == str[mid + 1]){
len2 = LenOfMaxPalindrome(str, len, mid - 1, mid + 2) + 2;
}
if (len1 < len2)
len1 = len2;
if (max < len1)
max = len1;
} return max;
} int main(void){
char str[200001]; while (scanf ("%s", str) != EOF){
printf ("%d\n", MaxPalindrome (str));
} return 0;
}
O(n)的回文子串Manacher算法(详细算法讲解见参考资料)
算法代码实现如下:
#include <stdio.h>
void Manacher (char str[], int len, int Radix[]){
int mx = 0; //记录被影响到的最远的位置
int id = 0; //最长影响串的位置
Radix[0] = 0;
int i;
for (i=1; i<len; ++i){
Radix[i] = 1;
if (mx > i){
Radix[i] = Radix[2 * id - i];
if (mx - i < Radix[i])
Radix[i] = mx - i;
}
while (str[i - Radix[i]] == str[i + Radix[i]])
++Radix[i];
if (i + Radix[i] > mx){
mx = i + Radix[i];
id = i;
}
}
}
int Preproccess (char str[], char old_str[]){
int index;
int len;
char middle = '#';
str[0] = '$';
str[1] = middle;
index = 0;
len = 2;
while (old_str[index]){
str[len++] = old_str[index++];
str[len++] = middle;
}
str[len] = '?';
return len;
}
int main(void){
char old_str[200001];
char str[400004];
int Radix[400004];
int len;
int i;
int ans;
while (scanf ("%s", old_str) != EOF){
len = Preproccess (str, old_str);
Manacher (str, len, Radix);
ans = 0;
for (i=1; i<len; ++i){
if (ans < Radix[i])
ans = Radix[i];
}
printf ("%d\n", ans - 1);
}
return 0;
}
参考资料:http://tiankonguse.com/blog/?p=84
http://www.cnblogs.com/biyeymyhjob/archive/2012/10/04/2711527.html
http://www.akalin.cx/longest-palindrome-linear-time
http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html
http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html
相思题目:https://oj.leetcode.com/problems/longest-palindromic-substring/
九度OJ 1528 最长回文子串 -- Manacher算法的更多相关文章
- 九度oj 1528 最长回文子串
原题链接:http://ac.jobdu.com/problem.php?pid=1528 小白书上的做法,不过这个还要简单些... #include<algorithm> #includ ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
- 最长回文子串—Manacher 算法 及 python实现
最长回文子串问题:给定一个字符串,求它的最长回文子串长度.如果一个字符串正着读和反着读是一样的,那它就是回文串. 给定一个字符串,求它最长的回文子串长度,例如输入字符串'35534321',它的最 ...
- hihocoder #1032 : 最长回文子串 Manacher算法
题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...
- 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- HiHo 1032 最长回文子串 (Manacher算法求解)
/** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...
- hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]
传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...
- 51nod1089 最长回文子串 manacher算法
0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字符串正着读和反着读是一样的,那它就是回文串.下面是一些回文串的实例: 12321 a aba abba aaaa ...
- 九度oj 题目1252:回文子串
题目描述: 输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. 输入: 存在多组数据,每组数据一行字 ...
随机推荐
- 剑指OFFER之字符串的排列(九度OJ1369)
题目描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入: 每个 ...
- iOS Instruments内存检测使用
Instruments 可以帮我们了解到应用程序使用内存的几个方面: 全局内存使用情况(Overall Memory Use): 从全局的角度监测应用程序的内存使用情况,捕捉非预期的或大幅度的内存增长 ...
- SQL Sever——无法连接到(local)。“未配置远程连接”和“请求失败或服务未及时响应”
攻克了上篇博客提到的"远程过程调用失败(0x800706be)"的问题. 新的问题接踵而至. . . 一. watermark/2/text/aHR0cDovL2 ...
- 小谈一下Java I/O
java中的I/O操作宏观上来说就氛围两个种类,一个是字节流 ,一个是字符流,分别相应着一组类和接口 字节流: InputStream 输入流的基类 OutputStream 输出流的基类 对于字节流 ...
- c++ primer,友元函数上的一个例子(By Sybase)
本文试图解释c++ primer Screen 和 Window_Mgr的例子,为什么将两个类放在两个文件中无法编译? 将两个类写在同一个文件中,通过三个例子解释问题: 第一种写法问题: 编译到Scr ...
- redis的hash操作在集中式session中的应用
在集群部署时,为了高可用性的目的,往往把session进行共享,共享分为两种:session复制和集中式管理. redis在session集中式管理中可以起到比较大的作用. 制约session集中式共 ...
- Linux SPI框架(下)
分类: Linux驱动程序2012-07-11 20:44 3006人阅读 评论(2) 收藏 举报 linuxstructlistclassdelayprocessing 水平有限,描述不当之处还请之 ...
- ios代理设计模式
代理设计模式的作用: 1.A对象监听B对象的一些行为,A成为B的代理 2.B对象想告诉A对象一些事情,A成为B的代理 代理设计模式的总结: 如果你想监听别人的一些行为,那么 ...
- 对于 NSLayoutConstraint 不执行动画的处理:
在开发中 我们有时候需要改变某个空间的约束条件 也就是更改NSLayoutConstraint的值 (比如说我想在键盘顶部增加一个工具栏 让工具栏随着键盘的位置变化而变化 有一个动画效果)但是发 ...
- linux commond
1 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2 ifconfig 3 ping 172.22.14.59 4 ping 1 ...