zoukankan      html  css  js  c++  java
  • 字符串对称

    题目:输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。

    #include<iostream>
    #include
    <string>
    using namespace std;

    int MaxLength(char* pString){
    if(NULL == pString)
    return 0;

    int maxLength = 1;
    char* pChar = pString;

    char* Position;
    int currentLength;

    while(*pChar != '\0'){
    char* pFirst = pChar-1;
    char* pLast = pChar+1;
    while(pFirst>=pString && *pLast != '\0' && *pFirst == *pLast){
    --pFirst;
    ++pLast;
    }

    currentLength
    = pLast-pFirst-1;
    if(currentLength>maxLength){
    maxLength
    = currentLength;
    Position
    = pChar;
    }

    pFirst
    = pChar;
    pLast
    = pChar+1;
    while(pFirst>=pString && *pLast != '\0' && *pFirst == *pLast){
    --pFirst;
    ++pLast;
    }

    currentLength
    = pLast-pFirst-1;
    if(currentLength>maxLength){
    maxLength
    = currentLength;
    Position
    = pChar;
    }

    pChar
    ++;
    }
    return maxLength;
    }

    int main(){
    char* pString = "ab";
    cout
    << "The length of pString: " << strlen(pString) << endl;

    cout
    << "The Longest symmetry of pString is: " << MaxLength(pString) << endl;

    return 0;
    }

  • 相关阅读:
    oc结构
    iOS分类
    iOS协议
    缓存无底洞现象
    数据库备份,恢复
    PHP邮件发送库:Swiftmailer
    PHP分页组件:Paginator
    PHP验证码类
    PHP日期和时间处理组件-Carbon
    composer的一些操作
  • 原文地址:https://www.cnblogs.com/phoenixzq/p/2129018.html
Copyright © 2011-2022 走看看