列表

详情


NC25593. Longest Palindrome Substring

描述

    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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 504K, 提交时间: 2019-06-01 15:43:12

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[100005];
int main(){
	int n;
	scanf("%d%s",&n,s);
	bool ans=0;
	for(int i=1;i<n;i++){
		if(s[i]==s[i-1]||i>=2&&s[i]==s[i-2]) ans=1;
	}
	if(ans) puts("YES");
	else puts("NO");
	return 0;
}

C(clang 3.9) 解法, 执行用时: 2ms, 内存消耗: 484K, 提交时间: 2019-06-01 15:13:52

#include <stdio.h>
int main()
{
	int n,i,flag=0;
	char s[100000];
	scanf("%d",&n);
	getchar();
	gets(s);
	for(i=0;i<n;i++){
		if(s[i]==s[i+1]||s[i]==s[i+2])
		{
			flag=1;
			break;
		}
	}
	if(flag==1){
		printf("YES\n");
	}
	else{
		printf("NO\n");
	}
 } 

C++11(clang++ 3.9) 解法, 执行用时: 9ms, 内存消耗: 632K, 提交时间: 2019-06-09 08:57:14

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	cin>>s;
	cin>>s;
	for(int i=0;i<s.length();i++){
		if(s[i]==s[i+1]||s[i]==s[i+2])
		{
			cout<<"YES"<<endl;
			return 0;
		}
	}
	cout<<"NO"<<endl;
} 

Python3 解法, 执行用时: 92ms, 内存消耗: 4868K, 提交时间: 2023-05-11 16:21:57

n=int(input())
x=' ' +input()+' '
for i in range(1,n):
    if x[i]==x[i+1] or x[i-1]==x[i+1]:
        print('YES')
        break
else:
    print('NO')

上一题