最长对称子串

最长对称子串

对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。

输入格式:

输入在一行中给出长度不超过1000的非空字符串。

输出格式:

在一行中输出最长对称子串的长度。

输入样例:

Is PAT&TAP symmetric?

输出样例:

11

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

 

#include <bits/stdc++.h>
using namespace std;
string str1;
int main(){
	int max1=0,max2=0;
	getline(cin,str1);
	int mid=str1.size()/2;
	int lon=str1.size();
	for(int i=0;i<=mid;i++){
		int count=-1;
		for(int j=0;j<=i;j++){
			if(str1[i-j]==str1[i+j]){
				count+=2;
			}else{
				break;
			}
		}
		int count2=0;
		if(mid!=0&&str1[i]==str1[i+1]){
			for(int j=0;j<=i;j++){
				if(lon>i+j+1&&str1[i-j]==str1[i+j+1]){
					count2+=2;
				}else{
					break;
				}
			}
			
		}
		if(max2<count2){
			max2=count2;
		}
		if(max1<count){
			max1=count;
		}
	}
	cout<<(max1>max2?max1:max2);
	return 0;
} 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值