C++ 关于字符串最大子序列的一个程序

C++ 关于字符串最大子序列的一个程序,c++,string,C++,String,我的问题叫做“最大子序列”。这是指导方针 字符串x的子序列可以通过删除其中的一些(可能全部或无)来生成 字母x。例如,“opt”是“computer”的子序列,而“rt”不是。 现在,我们想从给定的字符串中找出字典的最大子序列。 例如,“test”的排序子序列是: “”(空字符串)、“e”、“es”、“est”、“et”、“s”、“st”、“t”、“te”、“tes”、“test”、“tet”、“ts”、“tst”和 “tt”。 “tt”是这里最大的子序列,所以打印出来 我已经完成了我的代码(如

我的问题叫做“最大子序列”。这是指导方针

字符串x的子序列可以通过删除其中的一些(可能全部或无)来生成 字母x。例如,“opt”是“computer”的子序列,而“rt”不是。 现在,我们想从给定的字符串中找出字典的最大子序列。 例如,“test”的排序子序列是: “”(空字符串)、“e”、“es”、“est”、“et”、“s”、“st”、“t”、“te”、“tes”、“test”、“tet”、“ts”、“tst”和 “tt”。 “tt”是这里最大的子序列,所以打印出来

我已经完成了我的代码(如下所示)。它在我的本地编译器中成功,但在提交时失败(运行时错误)

#包括
#包括
#包括
使用名称空间std;
字符最大(字符串s){
字符c='a';
对于(int i=0;ic){
c=s.at(i);
}
}
返回c;
}
字符串橡皮擦STR(字符串s,字符c){
对于(int i=0;inumCase;
for(int i=0;i>s[i];

cout此行可能出现运行时错误:

cin >> s[i];
如果
i>=100
,则可能出现SEGFULT。 您需要在这里动态分配—我建议您了解
std::vector
,例如,您可以这样做

int main() {

    int numCase;
    cin >> numCase;
    std::vector<string> s(numCase);
    for (int i = 0; i < numCase; i++) {
        cin >> s[i];
//...
intmain(){
内切酶;
cin>>核酸酶;
std::载体s(numCase);
for(int i=0;i>s[i];
//...

可能还有其他运行时错误,这只是我spottet的第一个错误。

如果您有运行时错误,则可能必须改进算法

这是一个O(nlogn)解决方案

该方案:

    #include <iostream>
    #include <vector>
    #include <string>
    #include <numeric>
    #include <algorithm>

    //  1. sort (in a stable way) indices according to string values
    //  2. first character corresponds to first index
    //  3. add following characters, if the index is greater than last index selected

    std::string max_subsequence (std::string &x) {
        std::string result;
        if (x.size() == 0) return result;

        std::vector<int> index (x.size());
        std::iota (index.begin(), index.end(), 0);
        std::stable_sort (index.begin(), index.end(), [&] (int i, int j) {return x[i] > x[j];});

        result.push_back(x[index[0]]);
        int last_index = index[0];
        for (int i = 1; i < x.size(); ++i) {
            if (index[i] > last_index) {
                result.push_back(x[index[i]]);
                last_index = index[i];
            }
        }
        return result;
    }

    int main() {
        std::string s = "test";
        auto result = max_subsequence (s);

        std::cout << "result = " << result << "\n";
        return 0;
    }
#包括
#包括
#包括
#包括
#包括
//1.根据字符串值对索引进行排序(以稳定的方式)
//2.第一个字符对应于第一个索引
//3.如果索引大于上次选择的索引,则添加以下字符
std::string max_子序列(std::string&x){
std::字符串结果;
如果(x.size()==0)返回结果;
向量索引(x.size());
std::iota(index.begin()、index.end()、0);
std::stable_sort(index.begin(),index.end(),[&](inti,intj){返回x[i]>x[j];});
结果:向后推(x[索引[0]]);
int last_index=索引[0];
对于(int i=1;i最后一个索引){
结果:推回(x[索引[i]]);
最后的指数=指数[i];
}
}
返回结果;
}
int main(){
std::string s=“测试”;
自动结果=最大子序列;
你所谓的“问题”是你想解决的练习。你的问题是什么?
    1. sort (in a stable way) indices according to string values
    2. first character corresponds to first index
    3. add following characters, if the index is greater than last index selected
    #include <iostream>
    #include <vector>
    #include <string>
    #include <numeric>
    #include <algorithm>

    //  1. sort (in a stable way) indices according to string values
    //  2. first character corresponds to first index
    //  3. add following characters, if the index is greater than last index selected

    std::string max_subsequence (std::string &x) {
        std::string result;
        if (x.size() == 0) return result;

        std::vector<int> index (x.size());
        std::iota (index.begin(), index.end(), 0);
        std::stable_sort (index.begin(), index.end(), [&] (int i, int j) {return x[i] > x[j];});

        result.push_back(x[index[0]]);
        int last_index = index[0];
        for (int i = 1; i < x.size(); ++i) {
            if (index[i] > last_index) {
                result.push_back(x[index[i]]);
                last_index = index[i];
            }
        }
        return result;
    }

    int main() {
        std::string s = "test";
        auto result = max_subsequence (s);

        std::cout << "result = " << result << "\n";
        return 0;
    }