1、输入一个只含有英文字母的字符串,输出最大回文子串的长度及此长度回文子串的个数(回文不区分大小写)。
样例:输入aBaAb (最大回文子串为BaAb)
输出4 1
输入aBcbBb (最大回文子串为Bcb和bBb)
输出 3 2
实际测试样例:
输入:a B
输出: 1 2
输入:aBcbB
输出:3 1
输入:aaaaaa
输出:6 1
#include<iostream>
#include<string>
#include<algorithm>
#define MAX 1000000
using namespace std;
int main()
{
string str;
while(getline(cin,str)&&str!="")
{
int max_len=0;
int num=0;
int len=str.length();
for(int i=0;i<len;i++)
{
for(int j=i+1;j<=len;j++)
{
string temp=str.substr(i,j-i);
int flag=0;
for(int k=0;k<(j-i)/2;k++)
{
if(temp[k]!=temp[j-i-k-1]&&abs(temp[k]-temp[j-i-k-1])!=32)
{
flag=1;
break;
}
}
if(flag==0)
{
if(j-i>max_len)
{
max_len=j-i;
num=1;
}
else if(j-i==max_len)
num++;
}
}
}
cout<<max_len<<" "<<num<<endl;
}
}
2、哥德巴赫猜想
任何一个大于2的偶数均可表示为两个素数之和。输入m, n(6<=m<=n<=50),则把[m, n]内的所有偶数表示成两个素数之和的形式。输出这些素数及其出现的次数,输出次序按照素数出现的次数从多到少输出;若出线次数相同,按照素数从大到小输出;若偶数有多种素数相加形式,则把所有的情况都输出,每种情况占一行。
输入:8 9 输出:5 1 3 1
输入:9 10 输出:5 2
7 1 3 1
输入:14 15 输出:7 2
11 1 3 1
输入:8 10 输出:5 3 3 1
3 2 7 1 5 1