zoukankan      html  css  js  c++  java
  • 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

    #include <iostream>
    #include<cstring>
    #include<string>
    #include<cstdio>
    using namespace std;
    
    int main()
    {
        string str;
        getline(cin,str);
        int size = str.size();
        int ans = 1;
        for(int i=0;i<size;i++)//左边开始的指针
        {
            for(int j=size-1;j>=i;j--)//右边开始的指针
            {
                if(str[i]==str[j])//左右相等就继续往里走
                {
    
    
                bool flag = true;//假设符合
               int t1 = i;
               int t2 = j;
    
                while(t1<=t2)//判断i,j之间是否全部对称
                {
                    t1++;
                    t2--;
                    if(str[t1]!=str[t2])//不相等就退出
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag )//i,j间全部对称就更新
                    ans = max(ans,j-i+1);
                }
            }
        }
    
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    ST (Sparse Table:稀疏表)算法
    P3379 【模板】最近公共祖先(LCA)
    AT1357 n^p mod m(洛谷)
    poj2018 Best Cow Fences
    P1024 一元三次方程求解
    poj2456
    poj1064
    P2047 [NOI2007]社交网络(洛谷)
    poj1734
    洛谷P2886 [USACO07NOV]牛继电器Cow Relays
  • 原文地址:https://www.cnblogs.com/qinmin/p/12990136.html
Copyright © 2011-2022 走看看