题目地址: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
来源:
腾讯2013年实习生招聘二面面试题

自己比较野蛮的方法

#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算法的更多相关文章

  1. 九度oj 1528 最长回文子串

    原题链接:http://ac.jobdu.com/problem.php?pid=1528 小白书上的做法,不过这个还要简单些... #include<algorithm> #includ ...

  2. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  3. 最长回文子串—Manacher 算法 及 python实现

    最长回文子串问题:给定一个字符串,求它的最长回文子串长度.如果一个字符串正着读和反着读是一样的,那它就是回文串.   给定一个字符串,求它最长的回文子串长度,例如输入字符串'35534321',它的最 ...

  4. hihocoder #1032 : 最长回文子串 Manacher算法

    题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...

  5. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. HiHo 1032 最长回文子串 (Manacher算法求解)

    /** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...

  7. hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]

    传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...

  8. 51nod1089 最长回文子串 manacher算法

    0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字符串正着读和反着读是一样的,那它就是回文串.下面是一些回文串的实例: 12321 a aba abba aaaa ...

  9. 九度oj 题目1252:回文子串

    题目描述: 输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. 输入: 存在多组数据,每组数据一行字 ...

随机推荐

  1. SQLite使用教程10 运算符

    SQLite 运算符 SQLite 运算符是什么? 运算符是一个保留字或字符,主要用于 SQLite 语句的 WHERE 子句中执行操作,如比较和算术运算. 运算符用于指定 SQLite 语句中的条件 ...

  2. JQuery.Ajax之错误调试帮助信息介绍

    下面是Jquery中AJAX参数详细列表: timeout Number 设置请求超时时间(毫秒).此设置将覆盖全局设置. async Boolean (默认: true) 默认设置下,所有请求均为异 ...

  3. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  4. Windows内核之进程基本含义以及进程的创建

    进程 1 进程的含义: 1.1   一个是操作系统用来管理进程的内核对象. 内核对象也是系统用来存放关于进程的统计信息的地方. 1.2   还有一个是地址空间,它包括全部可运行模块或DL L 模块的代 ...

  5. Tomcat 生产服务器性能优化

    虑一下这种场景,你开发了一个应用,它有十分优秀的布局设计,最新的特性以及其它的优秀特点.但是在性能这方面欠缺,不管这个应用如何都会遭到客户拒绝.客户总是期望它们的应用应该有更好的性能.如果你在产品中使 ...

  6. [017]string类使用注意事项

    最近自己写着玩,写了一个这样的函数: void foo(const string& iStr) { ; i < iStr.length(); ++i) { string str = iS ...

  7. js获取光标位置例子

    <html><head><title>TEST</title><style>body,td { font-family: verdana, ...

  8. 简单改写SQL达到优化目的

    select * from (select t.row_id, t.supplier_name, t.tel, address, t.contact, t.contact_post, t.contac ...

  9. 对JavaScript对象数组按指定属性和排序方向进行排序

    引子 在以数据为中心的信息系统中,以表格形式展示数据是在常见不过的方式了.对数据进行排序是必不可少的功能.排序可以分为按单个字段排序和按多个字段不同排序方向排序.单字段排序局限性较大,不能满足用户对数 ...

  10. Activity的启动模式及回退栈的概念

    Activity的启动模式 standard 正常模式 在创建一个新的activity的时候,直接在栈顶创建一个新的activity singleTop 顶部单个 在创建一个新的activity的时候 ...