您的位置:首页 > 其它

求最大回文子串的长度

2015-12-26 23:39 274 查看
输入一串随机字符串,找出并打印其中最大回文子串的长度,回文子串指的是正反读都一样的子串如"abcdedcba"

#include "stdafx.h"

#include<stdio.h>

#include<string.h>

void main()

{

int count = 0;

char s[30];

int i, l, max = 0, k, j, locate;

printf("Input the string:");

gets(s);

l = strlen(s);

for(i=1;i<l;i++)

{

for(k =i-1,j =i+1;k>=0&&s[k]==s[j];k--,j++);

if (i-k>max)

{

max =(i-k)*2+1;

locate =k+1;

}

}

for (i = 1; i<l;i++)

{

for(k = i-1,j=i;k>=0&&s[k]==s[j];k--,j++);

if (i-k>max)

{

max = (i-k)*2;locate=k+1;

}

}

printf("The max string is: \n");

for (i = locate; i < locate + max - 2; i++)

{

printf("%c", s[i]);

count++;

}

printf("\n");

printf("The length is: %d", count);

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: