天梯杯 最长对称子串 (25 分

版权声明:如果转载,请注明出处。 https://blog.csdn.net/S_999999/article/details/88353660

最长对称子串 (25 分)

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

输入格式:

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

输出格式:

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

输入样例:

Is PAT&TAP symmetric?

输出样例:

11
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
char  str[1009];
int   main(void){
	
	 string str;
	 getline( cin , str);
	 int cnt = str.length();
	 int maxx = 0;
	 if( str.length() == 1 )
	     printf("1\n");
	 else { 
	    for( int  i=1 ; i< cnt;i++){ 
		     for( int j=0; j<=i;j++){
		      int num = i - j  + 1;
			  if( num & 1 ){
                  int c1 = j  , c2 = i;
			  	  while( c2 >= c1 && str[c1++] == str[c2--] )
			  	  
				  if( c1 > c2 )
				      maxx =  max( maxx , num );      
			  }  
			  else {
			  	  int c1 = j ,c2  = i;
			  	  while( c2 > c1  && str[c1++] == str[c2--])
			  	  if( c1 > c2 )
			  	      maxx = max( maxx ,num ); 
			  } 	  
		 	    
		 } 
	}
    if(  maxx <= 0  )
         printf("0\n");
	else printf("%d\n",maxx);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/S_999999/article/details/88353660