zoukankan      html  css  js  c++  java
  • 1088 最长回文子串

    基准时间限制: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;
    }
  • 相关阅读:
    645. 错误的集合
    88. 合并两个有序数组
    125. 验证回文串
    常用的浏览器
    网页的相关概念
    HTML简介
    商城搜索解决方案
    用VirtualBox安装Centos7
    Eureka自我保护机制
    服务发现Discovery(查看运行的服务)
  • 原文地址:https://www.cnblogs.com/NaCl/p/9580213.html
Copyright © 2011-2022 走看看