zoukankan      html  css  js  c++  java
  • CCCC L2-008. 最长对称子串

    https://www.patest.cn/contests/gplt/L2-008

    题解:想法是扫一遍string,将每一个s[i]作为对称轴,写一个判定函数:不断向两边延伸直到不是回文串为止。

              然后发现没有考虑形如werrew的回文串,所以改了一下,注意两个判定函数的循环方式

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <map>
    #include<stack>
    #include<set>
    #include<string.h>
    #include<list>
    #define pb push_back
    #define mp make_pair
    #define _for(i, a, b) for (int i = (a); i<(b); ++i)
    #define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
    
    using namespace std;
    const  int N = 50 + 5;
    int len;
    string s;
    int test(int i) {
        int l =i, r = i;
        int cnt = 0;
        while (l-1 >= 0 && r +1<= len-1) {
            l--; r++;
            if (s[l] == s[r])cnt++;
            else break;
        }
        return cnt;
    }
    int test2(int i) {
        int l = i, r = i + 1;
        int cnt = 0;
        while (l  >= 0 && r  <= len - 1) {
            if (s[l] == s[r])cnt++;
            else break;
            l--; r++;
            
        }
        return cnt;
    }
    int main() {
        
        getline(cin, s);
         len = s.length();
         int mx = 0;
        //1000*500
        _for(i, 0, len) {
            mx = max(mx, test(i));
        }
        
        
        int ans=mx*2+1;
        mx = 0;
        _for(i, 0, len) {
            mx = max(mx, test2(i));
        }
        ans = max(ans, mx * 2);
        cout << ans << endl;
        system("pause");
    }
    /*qwerrewq*/
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    单例模式
    eclipse部署web项目至本地的tomcat但在webapps中找不到
    使用 google gson 转换Timestamp为JSON字符串
    前端JS对后台传递的timestamp的转换
    2018第15周总结
    Tomcat的最大并发数
    平台对接的另外一种模式
    系统间数据交换的5种方式
    Spring Boot条件注解
    Spring Data JPA 和MyBatis比较
  • 原文地址:https://www.cnblogs.com/SuuT/p/8670305.html
Copyright © 2011-2022 走看看