求最大回文子串的长度
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);
}
#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);
}
相关文章推荐
- 山东省第四届蓝桥杯 ///标题: 马虎的算式//c/c++组
- 算法之美:动态规划
- Android面试题总结加强再加强版(四)
- Linux netstat命令详解
- 在eclipse中进行Struts2项目的配置
- 【懒程序员的日常】接口文档绝对是个混账!
- BeanUtils.populate的作用
- MySQL常用字符集
- 一身正气姥爷之幽默谈开博!
- linux service命令
- 本地通知
- MyBatis Generator 详解
- 【跟我学spring 4.0 】之第三节-bean的配置
- Fragment不同操作的生命周期
- lua中的closure(闭合函数)
- 简单c语言习题:矩阵求乘积
- fast-rcnn的例子执行和selective search中遇到的问题及解决(二)
- GreenDao 教程
- JavaScript中的对象及层级关系、常用函数
- 浅谈参观SpeedyCloud云计算公司感想