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;
  • 相关阅读:
    IBatis简介
    cntlm代理使用
    bash快捷键你知道几个?
    django的Form中添加属性
    EMACS 中文显示为方框
    git合并子树
    算法 排序 python 实现堆排序
    android org.eclipse.wst.sse.core 0.0.0' but it could not be found
    我的EMACS配置
    python 输入# 自动跳到行首
  • 原文地址:https://www.cnblogs.com/For-her/p/3785578.html
Copyright © 2011-2022 走看看