题目提交
给定一个字符串,输出所有长度至少为 22 的回文子串。
回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:abba,cccdeedccc都是回文字符串。
输入格式
一个字符串,由字母或数字组成。长度 500500 以内。
输出格式
输出所有的回文子串,每个子串一行。
子串长度小的优先输出,若长度相等,则出现位置靠左的优先输出。
输出时每行末尾的多余空格,不影响答案正确性
样例输入
123321125775165561
样例输出
33
11
77
55
2332
2112
5775
6556
123321
165561
题解:若长度相等,则出现位置靠左的优先输出。则按照长度进行从左到右遍历。
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
char a[510];
int main()
{
int i,j,len,d,begin,maxx;
scanf("%s",a);
d=strlen(a);
for(len=2; len<=d; len++)
{
maxx=d-len;
for(begin=0; begin<=maxx; begin++)
{
j=begin+len-1;
for(i=begin; i<j; i++,j--)
{
if(a[i]!=a[j])
break;
}
if(i>=j)
{
j=begin+len-1;
for(i=begin; i<=j; i++)
printf("%c",a[i]);
printf("\n");
}
}
}
return 0;
}