在上一篇的文章中说到了,最长回文子串的问题,并且提到了基本的解决办法,即暴力求解法。效率O(N^3)

中心法求最长回文子串

我们知道回文字符串是以字符串中心对称的,如abba以及aba等。一个更好的办法是从中间开始判断,因为回文字符串以字符串中心对称。一个长度为N的字符串可能的对称中心有2N-1个,至于这里为什么是2N-1而不是N个,是因为可能对称的点可能是两个字符之间,比如abba的对称点就是第一个字母b和第二个字母b的中间。因此可以依次对2N-1个中心点进行判断,求出最长的回文字符串即可。

所以,要考虑回文是奇数还是偶数的情况。

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAXN 500+10
char buf[MAXN],s[MAXN];
int index[MAXN]; int main()
{
int i,j;
int start = ,end = ;
int max = ;
int m = ,n;
fgets(buf,sizeof(s),stdin);
n = strlen(buf); //剔除其中的非字母字符,并且全部转换为大写字母
for(i = ;i<n;i++)
{
if(isalpha(buf[i]))
{
index[m] = i;
s[m++] = toupper(buf[i]);
}
}
for(i = ;i<m;i++)
{
/**奇数情况下*/
/**以i为中心,向左向右移动j,判断是否相同*/
/**这样求出的长度就是2*j+i*/
for(j = ;i+j<m && i-j>=;j++)
{
if(s[i-j] != s[j+i]) break;
if(*j+ > max)
{
max = *j+;
start = i-j;
end = i+j;
}
}
/**偶数情况下*/
/**以i 和 i+1为中心,左右移动j,再判断*/
for(j = ;i-j>= && i++j<m;j++)
{
if(s[i-j] != s[i++j]) break;
if(*j+ > max)
{
max = *j+;
start = i-j;
end = i+j+;
}
}
}
printf("max = %d\n",max);
for(i = index[start];i<=index[end];i++)
printf("%c",buf[i]);
putchar(); return ;
}

C-最长回文子串(2)的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  2. 最长回文子串(Longest Palindromic Substring)

    这算是一道经典的题目了,最长回文子串问题是在一个字符串中求得满足回文子串条件的最长的那一个.常见的解题方法有三种: (1)暴力枚举法,以每个元素为中心同时向左和向右出发,复杂度O(n^2): (2)动 ...

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

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

  4. 1089 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa ...

  5. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

  6. 求最长回文子串:Manacher算法

    主要学习自:http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html 问题描述:回文字符串就是左右 ...

  7. [译+改]最长回文子串(Longest Palindromic Substring) Part II

    [译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...

  8. [译]最长回文子串(Longest Palindromic Substring) Part I

    [译]最长回文子串(Longest Palindromic Substring) Part I 英文原文链接在(http://leetcode.com/2011/11/longest-palindro ...

  9. Manacher's algorithm: 最长回文子串算法

    Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abcc ...

  10. 【转】最长回文子串的O(n)的Manacher算法

    Manacher算法 首先:大家都知道什么叫回文串吧,这个算法要解决的就是一个字符串中最长的回文子串有多长.这个算法可以在O(n)的时间复杂度内既线性时间复杂度的情况下,求出以每个字符为中心的最长回文 ...

随机推荐

  1. Cstyle的札记,Freertos内核具体解释,第0篇

        Freertos是一个硬实时内核,支持众多的微处理器架构,我们能够从它的官网(www.freertos.ort)下载它的sourcecode,同一时候也能够看出它支持了几十种的微处理器架构,这 ...

  2. .net 常用方法

    1.String数组转换成Int数组 string[] strArr =  "a,b,c".Split(','); int[] intArr = Array.ConvertAll& ...

  3. Eclipse使用笔记

    eclipse内容辅助键 alt+ /用法: Alt+/ 提示作用 帮助补齐一些东西,还可以帮助你起名字, main+alt+/,syso+alt+/ alt+shift+s给出一些快捷操作,比如fo ...

  4. 对象作为返回值类型&&链式编程

    package com.imooc; class Student{ public void study(){ System.out.println("学生学习"); } } cla ...

  5. Notepad++中Windows,Unix,Mac三种格式

    Notepad++中Windows,Unix,Mac三种格式之间的转换 http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htm ...

  6. JQuery隔行变色

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  7. Windows SDK笔记(经典--一定要看)

    Windows SDK笔记(一):Windows程序基本结构 一.概述 Windows程序具有相对固定的结构,对编写者而言,不需要书写整个过程,大部分过程由系统完成.程序中只要按一定的格式填写系统留给 ...

  8. jQuery报错:Uncaught ReferenceError: $ is not defined

    在使用jQuery的时候,发现有如下报错: Uncaught ReferenceError: $ is not defined  (anonymous function) 出现这个报错的原因: 1.j ...

  9. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  10. Oracle Licensing

    Oracle根据什么来计算License的? Unlimited License Agreements Unlimited License Agreements通常简称ULA,表示在一个固定期限内(2 ...