• 51Nod-1088-最长回文子串


    51Nod-1088-最长回文子串

    回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。
    输入一个字符串Str,输出Str里最长回文子串的长度。
    Input
    输入Str(Str的长度 <= 1000)
    Output
    输出最长回文子串的长度L。
    Input示例
    daabaac
    Output示例
    5

    使用暴力破解,直接穷举得到answer。

    #include <cstdio> 
    #include <cstring> 
    const int MAXN = 1000 + 10; 
    
    #define max(a, b) (a)>(b)?(a):(b) 
    
    int main(){
    	char ch[MAXN]; 
    
    	scanf("%s", ch); 
    	int ans = 1, len = strlen(ch); 
    
    	for(int i=0; i<len; ++i){
    		int k = 1; 
    		while( i-k>=0 && i+k<len && ch[i-k] == ch[i+k] ){ 
    			ans = max( ans, 2*k + 1 ); 
    			++k; 
    		} 
    		if(i + 1 < len && ch[i] == ch[i+1]){
    			k = 1; 
    			ans = max(ans, 2); 
    			while(i-k>=0 && i+k+1<len && ch[i-k] == ch[i+1+k]){ 
    				ans = max(ans, 2*k + 2); 
    				++k; 
    			}
    		}
    	} 
    	printf("%d
    ", ans );
    
    	return 0; 
    }
    

      

  • 相关阅读:
    Python-time和datetime模块
    Python-hashlib模块
    Python-利用flask模块创建web接口
    Python-操作Excel
    2
    1
    8
    7
    HDFS元数据管理实战篇
    使用HttpFS网关从防火墙后面访问HDFS
  • 原文地址:https://www.cnblogs.com/zhang-yd/p/7699861.html
Copyright © 2020-2023  润新知