34:回文子串
- 总时间限制:
- 1000ms 内存限制:
- 65536kB
- 描述
-
给定一个字符串,输出所有长度至少为2的回文子串。
回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:abba,cccdeedccc都是回文字符串。
输入 - 一个字符串,由字母或数字组成。长度500以内。 输出
- 输出所有的回文子串,每个子串一行。 子串长度小的优先输出,若长度相等,则出现位置靠左的优先输出。 样例输入
-
123321125775165561
样例输出 -
331177552332211257756556123321165561
来源 - 习题(12-6)
思路:
暴力模拟;
来,上代码:
#include#include #include #include #include using namespace std;int len;string word;inline bool check(int l,int r){ while(r>l) { if(word[l]!=word[r]) return false; r--,l++; } return true;}inline void print(int l,int r){ for(int i=l;i<=r;i++) putchar(word[i]); putchar('\n');}int main(){ cin>>word; len=word.length(); for(int i=2;i<=len;i++) { for(int j=0;j<=len-i;j++) { if(check(j,j+i-1)) print(j,j+i-1); } } return 0;}