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;
    }
  • 相关阅读:
    百度搜索技巧
    phpstorm知识点
    A-Z
    边框
    display
    布局
    盒模型
    浮动
    字体与图标
    pselect 问题
  • 原文地址:https://www.cnblogs.com/DJC-BLOG/p/9558152.html
Copyright © 2011-2022 走看看