zoukankan      html  css  js  c++  java
  • PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

    1040 Longest Symmetric String (25 分)
     

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.

    Input Specification:

    Each input file contains one test case which gives a non-empty string of length no more than 1000.

    Output Specification:

    For each test case, simply print the maximum length in a line.

    Sample Input:

    Is PAT&TAP symmetric?
    

    Sample Output:

    11

    题意:

    输入一个字符串,求该字符串中最长对称子串的长度。

    题解:

    穷举搜索,既要考虑  baab这种偶数类型的,也要考虑abcba这种技术类型的。

    AC代码:

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<string>
    #include<cstring>
    using namespace std;
    string a;
    int main(){
        getline(cin,a);
        int len=a.length();
        int mx=1;
        //先偶数
        int r=0,l=1;
        int k;
        for(r=0;r<len;r++)
        {
            l=r+1;
            k=0;
            int rr=r;
            int ll=l;
            while(rr>=0&&ll<len&&a[rr]==a[ll]){
                k+=2;
                rr--;ll++;
            }
            mx=max(k,mx);
        }
        //再奇数
        for(r=0;r<len;r++)
        {
            l=r+2;
            k=1;
            int rr=r;
            int ll=l;
            while(rr>=0&&ll<len&&a[rr]==a[ll]){
                k+=2;
                rr--;ll++;
            }
            mx=max(k,mx);
        }
        cout<<mx<<endl;
        return 0;
    }
  • 相关阅读:
    天大复试机试练习_003
    C++随手记--字符串转数字
    C++标准库STL 之 我觉得应该有的方法——split
    apt-get 详解&&配置阿里源
    Nginx 图文详解
    MySQL数据库管理常用命令小结
    oracle数据库备份
    SqlServer数据库备份还原步骤
    mysql数据备份与恢复
    Tomcat架构
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270653.html
Copyright © 2011-2022 走看看