zoukankan      html  css  js  c++  java
  • 算法笔记(c++)--回文

                      算法笔记(c++)--回文


     

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    int main()
    {
        std::string s;
        char h[1000];
        int max=0,start,last;
        int temp[1000],m=0;
        getline(cin,s);
        //这里的temp保存了原字符值在原字符串的位置,可以借鉴下
        for(int i=0;i<s.length();i++)
            if(isalpha(s[i]))
            {
                temp[m]=i;
                h[m++]=tolower(s[i]);
            }
        for(int i=0;i<m;i++)
        {
            //奇数
            for(int j=0;i-j>0&&i+j<m;j++)
            {
                if(h[i-j]!=h[i+j])
                    break;
                if(j*2+1>max)
                {
                    max=j*2+1;
                    start=temp[i-j];
                    last=temp[i+j];
                }
            }
            //偶数
             for(int j=0;i-j>0&&i+j+1<m;j++)
             {
                if(h[i-j]!=h[j+i+1])
                    break;
                if(max<j*2+2)
                {
                    max=j*2+2;
                    start=temp[i-j];
                    last=temp[i+j+1];
                }
             }
        }
        for(int i=start;i<=last;i++)
            cout<<s[i];
        cout<<max;
        return 0;
    }
  • 相关阅读:
    Delphi语法
    orcad中注意的事情
    Express web框架
    Docker
    Node.JS
    再次 WebAssembly 技术探讨
    WebAssembly 浏览器中运行c/c++模块
    Http 服务 简单示例
    CentOS7 开放服务端口
    Go linux 实践4
  • 原文地址:https://www.cnblogs.com/DJC-BLOG/p/9558152.html
Copyright © 2011-2022 走看看