6. 最长回文子串

  1. 6. 最长回文子串

【问题描述】给定一个字符串s,找出s里第一次出现的最长的回文子串。

【样例输入1】babad
【样例输出1】bab

【样例输入2】cbbd
【样例输出2】bb

#include <iostream>
#include <cstring>
using namespace std;
struct ma
{
    char b[100];
    int len = 0;
} a, temp;
bool ju(char s[], int n)
{
    int i;
    for (i = 0; i <= n / 2; i++)
        if (s[i] != s[n - i])
            return false;
    return true;
}
int main()
{
    int size, c = 0;
    char str[1000];
    int i = 0;
    cin.getline(str, 1000);
    size = strlen(str);
    int start, t, j;
    for (i = 2; i <= size; i++)
    {
        for (j = 0, t = i; t <= size; j++, t++)
        {
            int w = j;
            for (int q = 0; q < i; q++)
            {
                temp.b[q] = str[w];
                w++;
            }
            temp.len = i;
            if (ju(temp.b, i - 1) && temp.len > a.len)
            {
                for (a.len = 0; a.len < temp.len; a.len++)
                    a.b[a.len] = temp.b[a.len];
            }
        }
    }
    for (i = 0; i < a.len; i++)
        cout << a.b[i];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wrong Ansewer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值