Longest Palindrome Substring

本文介绍了一种简单的方法来判断一个字符串中是否存在长度大于1的回文子串。通过两次遍历,首先检查相邻字符是否相等,然后检查间隔一位的字符是否相等,以此来快速确定字符串中是否存在回文子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://ac.nowcoder.com/acm/contest/908/G
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

    A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. For example, ”a”、”aba”、“abba” are palindrome and “abc”、”aabb” are not.

    Let’s define a new function f(s).

    For some string s, f(s) is the length of the longest palindrome substring.

    Now you should decide for the given string s, whether f(s) is great than 1.

    The string s only contains lowercase letters.

输入描述:

The first line of the input contains one integer n ------ the length of the string (1<=n<=10^5)

The second line of the input is the string.

输出描述:

If f(s) great than 1, print “YES” without quote

Else print “NO” without quote

示例1

输入

复制

4
abcd

输出

复制

NO

示例2

输入

复制

4
abcb

输出

复制

YES

题意:判断给出串中是否存在长度大于1的回文子串。

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    int n;
    char str[100050];
    cin>>n>>str;
    int fg = 0;
    for(int i = 1; i < n; i++)
    {
        if(str[i]==str[i-1])
        {
            fg = 1;
            break;
        }
    }
    if(fg==1)
    {
        cout<<"YES"<<endl;
        return 0;
    }

    for(int i = 1; i < n-1; i++)
    {
        if(str[i-1] == str[i+1])
        {
            fg = 1;
            break;
        }
    }
    if(fg == 1)
    {
        cout<<"YES"<<endl;
        return 0;
    }
    cout<<"NO"<<endl;

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值