名人说:故立志者,为学之心也;为学者,立志之事也。—— 王阳明
进度:C/C++语言100题练习计划专栏,目前55/100
🥇C/C++语言100题练习专栏计划:目的:巩固练习C/C++语言,增强上机、动手实践能力,交流学习!
一、问题呈现
1.问题描述
Problem Description
给定一个字符串,输出所有长度至少为2的回文子串。
回文子串即从左往右输出和从右往左输出结果是一样的字符串,比如:abba,cccdeedccc都是回文字符串。
2.输入输出
Input
一个字符串,长度1200以内。
Output
输出所有的回文子串,每个子串一行。
子串长度小的优先输出,若长度相等,则出现位置靠左的优先输出。
3.测试样例
样例1
Sample Input
123321125775165561
Sample Output
33
11
77
55
2332
2112
5775
6556
123321
165561
样例2
Sample Input
12344321
Sample Output
44
3443
234432
12344321
二、源码实现
#include<iostream>
#include<cstring>
using namespace std;
string str;
int len;
//回文串函数
int Palindrome(int x,int y)
{
int s = x,t = y;
while(s<=t)
{
if(str[s]!=str[t])
{
return 0;
}
s++,t--;
}
for(int i=x;i<=y;i++)
{
cout<<str[i];
}
cout<<endl;
return 0;
}
//主函数
int main()
{
getline(cin,str);//输入字符串
len = str.size();//求得字符串的长度 并赋值给len
//循环遍历查找所有长度至少为2的回文子串,将其输出
for(int k=1;k<len;k++)
{
for(int i=0;i<=len-k;i++)
{
Palindrome(i,i+k);
}
}
return 0;
}
三、测试结果
123321125775165561
33
11
77
55
2332
2112
5775
6556
123321
165561
--------------------------------
Process exited after 1.452 seconds with return value 0
请按任意键继续. . .
Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder)
如果对大家有帮助的话,希望大家能多多点赞+关注!这样我动力会更足哦! ღ( ´・ᴗ・` )比心