回文子串

回文子串

总时间限制: 
1000ms
内存限制: 
65536kB
描述

给定一个字符串,输出所有长度至少为2的回文子串。

回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:abba,cccdeedccc都是回文字符串。

输入
一个字符串,由字母或数字组成。长度500以内。
输出
输出所有的回文子串,每个子串一行。
子串长度小的优先输出,若长度相等,则出现位置靠左的优先输出。
样例输入
123321125775165561
样例输出
33
11
77
55
2332
2112
5775
6556
123321
165561
 
    
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

int main(){
    char a[300];
    scanf("%s",a);
    int b=strlen(a);
    int i,j,k,d,e,c;
    for(i=2;i<=b;i++){ //i是现在回文的长度
        c=b-i; //c是最后结束时回文开头的位置
        for(j=0;j<=c;j++){ //j回文开始的头
            e=j+i-1; //e是回文结束的位置
            for(k=j;k<e;k++,e--){ //判断是否为回文
                if(a[k]!=a[e]) break;
            }
            if(k>=e){
                e=j+i-1;
                for(d=j; d<=e; d++)
					cout<<a[d];
				cout<<endl;
            }
        }
    }

    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_41199327/article/details/79943493