题意:给你一个字符串,判断他的最大回文子串的长度是否大于1,是输出YES,不是输出NO。
突破口:小小的思维,只需判断字符串中是否有长度为2或者为3的字符串就好了,因为最大回文子串从中间延展开来的部分也一定是回文串。
代码如下:
#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
const int maxn=1e5+5;
stack<char> p; //利用栈来维护,可以将算法复杂度控制在O(n)
char s[maxn];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",s);
int flag=0,first=1;
char s_top; //s_top是用来记录栈顶底下第一个元素,即第二个栈顶
p.push(s[0]);
for(int i=1;i<=n-1;i++)
{
if(first)
{
first=0;
if(s[i]==p.top())
{
flag=1;
break;
}
else
{
s_top=p.top();
p.push(s[i]);
}
}
else
{
if(s[i]==p.top()||s[i]==s_top)
{
flag=1;
break;
}
else
{
s_top=p.top();
p.push(s[i]);
}
}
}
if(flag) printf("YES\n");
else printf("NO\n");
return 0;
}
这道题跟数据结构教材里那道栈的括号匹配检验的应用很相似。