zoukankan      html  css  js  c++  java
  • pat 团体赛练习题集 L2-008. 最长对称子串

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

    输入格式:

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

    输出格式:

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

    输入样例:
    Is PAT&TAP symmetric?
    
    输出样例:
    11
    

    虽然是水题,但还是错了几遍。贴上来警示下自己。

    错误原因:没考虑到对称串是偶数的情况。

    //Asimple
    #include <bits/stdc++.h>
    #define INF 0xfffffff
    #define mod 10007
    #define swap(a,b,t) t = a, a = b, b = t
    #define CLS(a, v) memset(a, v, sizeof(a))
    #define debug(a)  cout << #a << " = "  << a <<endl
    using namespace std;
    inline int abs(int x) { return x<0?-x:x; }
    typedef long long ll;
    const int maxn = 1005;
    char str[maxn];
    
    void solve() {
        int len = strlen(str);
        int ans = 1, cnt;
        for(int i=0; i<len; i++) {
            int in = 1, cnt = 1;
            while( i-in>=0 && i+in<len && str[i-in]==str[i+in] ) {
                in ++;
                cnt += 2;
            }
            ans = max(ans, cnt);
            in = 1, cnt = 0;
            while( i-in>=0 && i+in-1<len && str[i-in]==str[i+in-1] ) {
                in ++;
                cnt += 2;
            }
            ans = max(ans, cnt);
        }
        cout << ans << endl;
    }
    
    void input() {
        gets(str);
        solve();
    }
    
    int main() {
        input();
        return 0;
    }
    低调做人,高调做事。
  • 相关阅读:
    String,StringBuffer与StringBuilder的区别?
    Digui
    Digui1
    逆序
    TestOverWrite
    DemoBoxWeight
    TestSuperSub
    Cast
    TestOverWrite
    Joseph
  • 原文地址:https://www.cnblogs.com/Asimple/p/6476501.html
Copyright © 2011-2022 走看看