zoukankan      html  css  js  c++  java
  • 1088 最长回文子串 分类: 51nod 2015-07-20 22:09 8人阅读 评论(0) 收藏

    基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题

    收藏

    关注

    回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。

    输入一个字符串Str,输出Str里最长回文子串的长度。

    Input
    输入Str(Str的长度 <= 1000)

    Output
    输出最长回文子串的长度L。

    Input示例
    daabaac

    Output示例
    5
    水题,暴力,但不知为何自己
    的代码很长

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char s[1000];
    int solve(char ch[])
    {
        int n=strlen(ch);
        int flag=0;
        for(int i=0;i<n/2;i++)
        {
            if(ch[i]!=ch[n-1-i])
            return 1; 
        }
        return 0;
    }
    int main()
    {
          char ch[1000];
         scanf("%s",s);
         int n=strlen(s);
         int ans=0,max=0;
         for(int i=0;i<n;i++)
         {
            for(int j=n-1;j>=i;j--)
            {
                int k=0;
                for(int o=i;o<=j;o++)
                {
                    ch[k++]=s[o];
                }
                ch[k]='';
                if(solve(ch)==0)
                {
                    ans=j-i;
                     if(ans>max)
                     max=ans;
                }
            }
         }
         printf("%d
    ",max+1);
         return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    牛客练习赛9
    Good Bye 2017
    Wannafly挑战赛6
    TOJ1840: Jack Straws 判断两线段相交+并查集
    Codeforces Round #452 (Div. 2)
    TOJ4505: KOSARE
    Codeforces Round #451 (Div. 2)
    牛客练习赛8
    TOJ4168: Same Digits
    TOJ4483: Common Digit Pairs
  • 原文地址:https://www.cnblogs.com/NaCl/p/4700591.html
Copyright © 2011-2022 走看看