Search code examples
c++stringc++17sliceprefix

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


strong text

#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;
}

**Given 3 strings S1, S2 and X, find the number of distinct ordered pairs of strings (P,Q) such that :

String P+Q is a substring of X.

String P is some prefix of S1 (possibly empty).

String Q is some prefix of S2 (possibly empty).

A substring of a string is a contiguous subsequence of that string. For example, "chef" is a substring of "codechef", but "def" is not. Also, an empty string is a substring of any string.

A prefix of a string S is a substring of S that occurs at the beginning of S. For example, "code" is a prefix of "codechef", but "chef" is not. Also, an empty string is a prefix of any string.**

**My approach-slicing the string s1 and s2 using substr function and then checking whether there are substring of x using a algorithm or boost function i have used s.insert(a+"0") it just signifies that a is a prefix of s1 string and we have taken null prefix i.e. "" from s2. Same s.insert(""+b) means null prefix from string s1 and b prefix from s2 and so on.

****Problem - It is giving wrong output for one inputset dont know why. Debug it and see the pic https://i.sstatic.net/zUm5O.png **


Solution

  • I cleaned up your main function a little and removed boost usage (since you don't really need it here). I've also removed your namespace declarations as that is a bad idea. Lastly, I replaced your boost usage with a simple string::find. You can find the live example here.

    The output is :

    4
    
    0b
    a0
    ab
    

    The updated main() is below with your example set, instead of using 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;
    }