最长回文子串,中心扩展法 -- C语言

需求

 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
 
 示例 1: 
 输入: "babad"
 输出: "bab"
 注意: "aba" 也是一个有效答案。

 示例 2:
 输入: "cbbd"
 输出: "bb"

 

思路

因为是找回文子串,所以使用中心扩展法,中心扩展有两种,一种是以单个字符[i,i]为中心,一个是以双个字符[i, i+1] 为中心,一旦发现首尾相同,就再向前后扩展一个,增大范围。知道扩展到原有字符串范围以外(left >=0 && right < n)。如果这是发现回文串更长,我们就将这个首尾和长度记录下来

 

代码实现

/*
 * 需求
 
 给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
 
 示例 1:
 
 输入: "babad"
 输出: "bab"
 注意: "aba" 也是一个有效答案。
 示例 2:
 
 输入: "cbbd"
 输出: "bb"
  
 gcc LongestPalindrome.c -g -o a.exe -DDEBUG

 */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#ifdef DEBUG
#define LOG(fmt, args...) fprintf(stdout, fmt, ##args)
#define BREAKER(a, b, c) breaker(a, b, c)
#else
#define LOG(fmt,...)
#define BREAKER(a, b, c)
#endif

#define TRUE        1
#define FALSE       0

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a))


char * longestPalindrome2(char * s){

	if(NULL == s){
		return NULL;
	}

	int len = 0;
	int len1 = 0, len2 = 0;
	int subStart = 0, subEnd = 0;
	int size = 0;
	int i = 0;
	char * sub = NULL;

	size  = strlen(s);
	for(i = 0; i < size; i++){
		len1 = expend(s, i, i); /*[i, i] 为中心*/
		len2 = expend(s, i, i+1); /*[i, i] 为中心*/
		len = MAX(len1, len2);

		if(len > subEnd - subStart + 1){
			/*
			 * 这里start多减去一个1,是因为在双中心的情况下,
			 * 向前的距离要少去掉一个,因为双中心的头占了一个
		     */
			subStart = i - (len - 1)/ 2; 
			subEnd = i + len / 2;
		}	
	}

	len = subEnd - subStart + 1 + 1; /*减去start 补充1,'\0'再补充1*/
	sub = (char *)malloc(len * sizeof(char));
	memcpy(sub, s + subStart, len - 1); /*最后一个是'\0', 不用拷贝,单独赋值*/
	sub[len - 1] = '\0';	/*字符串结尾*/

	return sub;
}



void testlongestPalindrome(void){
	
	printf("\n************  testlongestPalindrome ************ \n");
	char str[100] = "babad";
	char str1[100] = "cbbd";
	char str2[100] = "a";
	char str3[100] = "";
	char str4[100] = "ac";
	char * s = NULL;
#if 1
	/*testcase 1*/
	s = longestPalindrome(str);
	if(NULL != s){
		printf("The longest Palindrome of %s is %s\n", str,s);
	}

	/*testcase 2*/
	s = longestPalindrome(str1);
	if(NULL != s){
		printf("The longest Palindrome of %s is %s\n", str1,s);
	}

	/*testcase 3*/
	s = longestPalindrome(str2);
	if(NULL != s){
		printf("The longest Palindrome of %s is %s\n", str2,s);
	}

	/*testcase 4*/
	s = longestPalindrome(str3);
	if(NULL != s){
		printf("The longest Palindrome of %s is %s\n", str3,s);
	}

	/*testcase 5*/
	s = longestPalindrome(str4);
	if(NULL != s){
		printf("The longest Palindrome of %s is %s\n", str4,s);
	}


	/*testcase 1*/
	s = longestPalindrome2(str);
	if(NULL != s){
		printf("The longest Palindrome2 of %s is %s\n", str,s);
	}


	/*testcase 2*/
	s = longestPalindrome2(str1);
	if(NULL != s){
		printf("The longest Palindrome2 of %s is %s\n", str1,s);
	}

	/*testcase 3*/
	s = longestPalindrome2(str2);
	if(NULL != s){
		printf("The longest Palindrome2 of %s is %s\n", str2,s);
	}

	/*testcase 4*/
	s = longestPalindrome2(str3);
	if(NULL != s){
		printf("The longest Palindrome2 of %s is %s\n", str3,s);
	}

	/*testcase 5*/
	s = longestPalindrome2(str4);
	if(NULL != s){
		printf("The longest Palindrome2 of %s is %s\n", str4,s);
	}
#endif

	return; 
 
 }


 int main(int argc, char ** argv){
	testlongestPalindrome();
 }

代码编译

 gcc LongestPalindrome.c -g -o a.exe -DDEBUG

 

调试输出


************  testlongestPalindrome ************
The longest Palindrome of babad is bab
The longest Palindrome of cbbd is bb
The longest Palindrome of a is a
The longest Palindrome of  is
The longest Palindrome of ac is c
The longest Palindrome2 of babad is bab
The longest Palindrome2 of cbbd is bb
The longest Palindrome2 of a is a
The longest Palindrome2 of  is
The longest Palindrome2 of ac is a

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值