简体   繁体   English

如何删除字符串中所有可能的子字符串中带有空格的子字符串?

[英]How to delete substrings with space in all possible substrings of a string?

The question is to find all possible substrings in a given string. 问题是在给定的字符串中找到所有可能的子字符串。

I know this question is somewhat similar to this. 我知道这个问题与此类似。

Generate all unique substrings for given string 生成给定字符串的所有唯一子字符串

But i'm trying it on my own. 但我自己尝试。 I have generated all possible combinations but problem is i want to eliminate some strings with space in them. 我已经生成了所有可能的组合,但是问题是我想消除一些带有空格的字符串。

int main()
{
    int t;
    cin>>t;
    while(t>0){
        long long int n;
        cin>>n;
        string S;
        cin>>S;
        for(int i=1; i<=S.size(); i++) {
            for(int j=0; j<=S.size()-1; j++) {

                cout<<S.substr(j,i)<<endl;
            }
        }

        t--;
    }
    return 0;
}

input: 输入:

1
3
abb 

Actual output: 实际输出:

a
b
b
ab
bb
b
abb
bb
b

Expected output: 预期产量:

a
b
b
ab
bb
abb

As you can see there are some terms with space in them which i want to eliminate like 6th,8th and 9th terms in Actual output. 如您所见,其中一些带有空格的术语要在实际输出中消除,例如第6、8和9个术语。

From the documentation of std::string::substr std::string::substr的文档中

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first). 子字符串是对象的一部分,从字符位置pos开始并跨越len个字符 (或直到字符串的结尾,以先到者为准)。

You are under the misconception that the second argument of std::string::substr is the end position, but in fact it is an offset from the first argument. 您可能会误以为std::string::substr的第二个参数是结束位置,但实际上它是第一个参数的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM