34:回文子串
总时间限制:
-
描述
-
给定一个字符串,输出所有长度至少为2的回文子串。
回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:abba,cccdeedccc都是回文字符串。
输入 - 一个字符串,由字母或数字组成。长度500以内。 输出
- 输出所有的回文子串,每个子串一行。
子串长度小的优先输出,若长度相等,则出现位置靠左的优先输出。
样例输入 -
123321125775165561
样例输出 -
33 11 77 55 2332 2112 5775 6556 123321 165561
-
-
#include <stdio.h> #include <iostream> #include <stack> #include <string.h> #include <queue> #include <cmath> #include <vector> #include <algorithm> #include <map> #include <set> #include <string> using namespace std; typedef long long LL; #define MAX 1000001 string str; int fun(int begin_t, int end_t) { while(end_t > begin_t){ if(str[end_t] != str[begin_t]){ return 0; } end_t--; begin_t++; } return 1; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); getline(cin, str); for(int i = 2; i <= str.size(); i++){ for(int j = 0; j <= str.size() - i; j++){ if(fun(j, j + i - 1)){ for(int k = j; k <= j + i - 1; k++){ cout << str[k]; } cout << endl; } } } return 0; }