zoukankan      html  css  js  c++  java
  • 最长对称子串

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

    输入格式:

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

    输出格式:

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

    输入样例:

    Is PAT&TAP symmetric?

    输出样例:

    11

    #include <bits/stdc++.h>
    using namespace std;
    string str;
    int ans;
    int len;
    int judge(int fore,int back,int slen){
        while(fore >= 0 && back < len){
            if(str[fore] == str[back])
                slen += 2,fore--,back++;
            else break;
        }
        ans = max(ans,slen);
        return ans;
    }
    int main(){
        //freopen("in","r",stdin);
        ios::sync_with_stdio(0);
        getline(cin,str);
        len = str.size();
        for(int i = 0; i < len; i++){
            //长度为奇数
            judge(i - 1, i + 1,1);
            //长度为偶数
            judge(i,i + 1,0);
        }
        cout << ans;
        return 0;
    }
    View Code
     
  • 相关阅读:
    第二章.md
    第四章.md
    第一章.md
    第九章.md
    png简析.md
    第五章.md
    好看的粒子效果
    缓动 减速运动
    收集的小效果
    粒子效果2
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12334352.html
Copyright © 2011-2022 走看看