尽管 aa 不是 ab 的子字符串,但为什么这段代码将 aa0 打印为 set 中的一个元素?

Why is this code printing aa0 as an element in set although aa is not a substring of ab?

强文本

#include <iostream>
#include<set>
#include <string>
#include <string_view>
#include <boost/algorithm/string.hpp>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main() 
{
     long int t;
     cin>>t;
     while(t--)
    {
        string s1,s2,x,a,b,f;
        cin>>s1>>s2>>x;
        std::set<string> s;
        int n,m,A;
        n=s1.length();
        m=s2.length();
        for(int i=0;i<n;i++)
        {
            a=s1.substr(0,i+1);
            if(boost::algorithm::contains(x, a));
            {
                s.insert(a+"0");
            }
            for(int j=0;j<m;j++)
            {
                b=s2.substr(0,j+1);
                if(boost::algorithm::contains(x,b))
                {
                    s.insert("0"+b);
                }
                f=a+b;
                if(boost::algorithm::contains(x,f))
                {
                    s.insert(f);
                }
            }
           
        }
        cout<<(s.size())+1<<endl;
            for (auto it = s.begin(); it !=
                             s.end(); ++it)
        cout << endl << *it;
    /*A=boost::algorithm::contains(x,"aa0");
        cout<<A;*/
    }
    return 0;
}

**给定 3 个字符串 S1、S2 和 X,找出不同的有序字符串对 (P,Q) 的数量,使得:

字符串 P+Q 是 X 的子字符串。

字符串 P 是 S1 的某个前缀(可能为空)。

字符串 Q 是 S2 的某个前缀(可能为空)。

字符串的子字符串是该字符串的连续子序列。例如,“chef”是“codechef”的子字符串,但“def”不是。此外,空字符串是任何字符串的子字符串。

字符串S的前缀是出现在S开头的S的子串。例如,“code”是“codechef”的前缀,但“chef”不是。此外,空字符串是任何字符串的前缀。**

**我的做法——用substr函数对字符串s1和s2进行切片,然后用算法或者boost函数判断x是否有子串 我使用了 s.insert(a+"0") 它只是表示 a 是 s1 字符串的前缀,我们从 s2 中获取了空前缀,即“”。相同 s.insert(""+b) 表示字符串 s1 的空前缀和 s2 的 b 前缀,依此类推。

****问题 - 它为一个输入集提供了错误的输出,不知道为什么。调试一下看图https://i.stack.imgur.com/zUm5O.png**

我稍微清理了你的 main 函数并删除了 boost 用法(因为你在这里并不真正需要它)。我还删除了您的名称空间声明,因为这是个坏主意。最后,我用一个简单的 string::find 替换了你的 boost 用法。您可以找到现场示例 here.

输出为:

4

0b
a0
ab

更新后的 main() 在您的示例集下面,而不是使用 std::cin。

#include <iostream>
#include <set>
#include <string>

int main() 
{
     long int t{1};
     while(t--)
    {
        std::string s1{"aa"},s2{"bb"},x{"ab"},a{""},b{""},f{""};
        std::set<std::string> s;
        int n,m,A;
        n=s1.length();
        m=s2.length();
        for(int i=0;i<n;i++)
        {
            a=s1.substr(0,i+1);
            if(x.find(a) != std::string::npos)
            {
                s.insert(a+"0");
            }
            for(int j=0;j<m;j++)
            {
                b=s2.substr(0,j+1);
                if(x.find(b) != std::string::npos)
                {
                    s.insert("0"+b);
                }
                f=a+b;
                if(x.find(f) != std::string::npos)
                {
                    s.insert(f);
                }
            }
           
        }
        std::cout<<(s.size())+1<<std::endl;
            for (auto it = s.begin(); it !=
                             s.end(); ++it)
        std::cout << std::endl << *it;
    /*A=boost::algorithm::contains(x,"aa0");
        cout<<A;*/
    }
    return 0;
}