zoukankan      html  css  js  c++  java
  • 随意输入N个英文字符,找出其中最长连续的排列。

     int out_max_length_crease_str(const char *p, std::vector<std::string> &vct)
    {
    vct.clear();
    int nlen = strlen(p);

    if (nlen == 0){
    return 0;
    }

    if (nlen == 1){
    vct.push_back(p);
    return 1;
    }

    char *buf = new char(nlen);
    memset(buf, 0, nlen);
    char first_char = *p;

    char *pbuf = buf;
    while (*++p != 0){
    if (*p > first_char){
    *pbuf++ = *p;
    }
    }

    if (pbuf == buf){
    std::string s;
    s += first_char;
    vct.push_back(s);
    return 1;
    }

    int nret = 0;
    std::vector<std::string> vct_temp;

    const char *temp = buf;
    for (; *temp != 0; ++temp){
    int ntemp = out_max_length_crease_str(temp, vct_temp);
    if (ntemp > nret){
    nret = ntemp;
    vct.swap(vct_temp);
    }
    else if (ntemp == nret){
    vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
    }
    }

    std::string s;
    for (std::string &str : vct){
    str.insert(str.begin(), first_char);
    }

    return nret + 1;
    }

    int out_max_length_crease_str_ex(const char *p, std::vector<std::string> &vct)
    {
    int max_lenth = 0;

    std::vector<std::string> vct_temp;
    const char *temp = p;
    for (; *temp != 0; ++temp){
    int lenth = out_max_length_crease_str(temp, vct_temp);
    if (lenth > max_lenth){
    max_lenth = lenth;
    vct.swap(vct_temp);
    }
    else if (lenth == max_lenth){
    vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
    }
    }
    return max_lenth;
    }
    int n = out_max_length_crease_str_ex("etphdumei", vct);

    n = out_max_length_crease_str_ex("vfleqiynp", vct);

    n = out_max_length_crease_str_ex("abdfghimo", vct);

    n = out_max_length_crease_str_ex("QMWBZGZGE", vct);
    只不过目前没有去重,最后一个例子,会返回几个重复的。

    int out_max_length_crease_str(const char *p, std::vector<std::string> &vct)
    {
    vct.clear();
    int nlen = strlen(p);

    if (nlen == 0){
    return 0;
    }

    if (nlen == 1){
    vct.push_back(p);
    return 1;
    }

    char *buf = new char[nlen];
    memset(buf, 0, nlen);
    char first_char = *p;

    char *pbuf = buf;
    while (*++p != 0){
    if (*p > first_char){
    *pbuf++ = *p;
    }
    }

    if (pbuf == buf){
    std::string s;
    s += first_char;
    vct.push_back(s);
    return 1;
    }

    int nret = 0;
    std::vector<std::string> vct_temp;

    pbuf = buf;
    for (; *pbuf != 0; ++pbuf){
    int ntemp = out_max_length_crease_str(pbuf, vct_temp);
    if (ntemp > nret){
    nret = ntemp;
    vct.swap(vct_temp);
    }
    else if (ntemp == nret){
    vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
    }
    }

    delete []buf;
    for (std::string &str : vct){
    str.insert(str.begin(), first_char);
    }

    return nret + 1;
  • 相关阅读:
    梯度消失和梯度爆炸
    BN的作用与使用过程
    百面机器学习笔记(二)
    正则表达式
    CSS Sprite
    事件绑定
    拖拽
    oncontextmenu
    鼠标跟随
    鼠标事件
  • 原文地址:https://www.cnblogs.com/For-her/p/3785578.html
Copyright © 2011-2022 走看看