SKMP - Editorial

In Last TT there is a typo
There are 2-s in input while 3 in output…please change it

Wow! thanks the second test case did the trick for me.

what’s wrong with my code ??
#include<bits/stdc++.h>

using namespace std;

int main()

{

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int t;

cin>>t;

while(t--)

{

    int flag=-1;

string s,p;

cin>>s>>p;

if(s==p)

cout<<s<<"endl";

else

{

for(int i=0;i<p.size();i++)

{

    for(int j=0;j<s.size();j++)

    if(p[i]==s[j])

    {

        s.erase(s.begin()+j);

        break;

    }

}

sort(s.begin(),s.end());

for(int i=0;i<s.size();i++)

{

    if(s[i]>p[0]){

    s.insert((i),p);

    flag=0;

    break;}

}

if(flag==-1)

{

   /* int j=0;

    sort(p.begin(),p.end());

    for(int i=0;i<s.size();i++)

    if(s[i]>p[j])

    {

        s.insert((i),p);

        j++;

    }*/

    cout<<s+p<<"\n";

}

else

cout<<s<<"\n";

}

}

}

Why TTT should be ‘bab’…why not ‘bba’?

for i in range(int(input())):
    s = input()
    p = input()
    s = list(s)
    p = list(p)
    a = []
    c = 0
    i = 0
    while(len(a) < len(p)):
        if p[i] in s:
            a.append(p[i])
            s.remove(p[i])
        i += 1
    #print(a)
    s.sort()
    if(a[0] == s[-1]):
        c = 1
    b = []
    i = 0
    while(len(s) > 0):
        if(c == 1):
            if(s[i] < a[0]):
                b.append(s[i])
                s.remove(s[i])
            else:
                a.append(s[i])
                s.remove(s[i])
        else:
            if(s[i]<=a[0]):
                b.append(s[i])
                s.remove(s[i])
            else:
                a.append(s[i])
                s.remove(s[i])
    b.sort()
    a = b+a
    a = ''.join(map(str,a))
    print(a)
    ```

My code run on sample testcases and yours testcases. But it's wrong in codechef. I dont get where I did mistake. Please any other testcase sir.

This is not my answer, this is given as example in editorial.

Check out Screencast Tutorial for this problem: https://www.youtube.com/watch?v=lrmhKZ0G1pk&list=PLz-fHCc6WaNIrJbPDdoWUscRqMZPEiqzQ

thanks to tell bro
corrected the error…

Can anyone help? getting WA on this code.
i have tested all these following test cases, given in above answers and getting right answer in my system. But still getting WA.

**3
ppqssstx
stx
ppqsastx
sa
ppqszstx
sz
6
zzzety
ze
acdeedaghfaee
eeaghfa
qwertyuioppppppoiuyt
tyuui
zaazyz
azy
jtewmpzqmscas
pqssc
zatgggg
gt
1
madammadam
madam
1
abb
ba
1
aaabbbdddd
ddab
1
abciiixaabb
iax

Answers:

ppqssstx
ppqsastx
ppqssztx

tyzezz
acddeeaghfaee
eiooppppppqrttyuuiwy
aazyzz
aejmmpqssctwz
aggggtz

aadmadammm

bab

aabbddabdd

aabbbciaxii**

https://www.codechef.com/viewsolution/36885633

Guys, please help me. Thanking you in advance… I just want to know what mistake I am doing…

https://www.codechef.com/viewsolution/36829794

pos is deciding factor that says whether to include non-contributing characters from ‘a’ to p[0] should append at the start of p OR not.refer above editorial twice-thrice (That’s how I understand :yum: most of the time)

1 Like

https://www.codechef.com/viewsolution/36911608
bro u chek this code it show tle in 2 test case

Hey i m getting 7/10 test cases passed on codechef
I am definitely missing a corner case
here is the link of my submissionmy_submission
@rishup_nitdgp @admin
pls help i m stuck badly

@anon20008309
your solution is similar to the solution which leak out in python…
and you are posting your solution in editorial
check this
@anon20008309 solution
https://www.codechef.com/viewsolution/36408808
other solution
https://www.codechef.com/viewsolution/36857962
https://www.codechef.com/viewsolution/36857738
and many more…
now i am reporting this to help@codechef.com

1 Like

This solution will not actually work. It will get TLE because your time complexity is O(n^2).
This is because you are using the python “remove” function which takes O(n) time. And you are using this function inside a for loop which also iterates n times.

Bro, if it was not working then why would I posted the solution here?
Just chill. It works just fine. :slight_smile:

Yes, exactly I was so shocked to see how it’s so similar :grimacing:

Whats wrong with this code?
https://www.codechef.com/viewsolution/36790365

I am not used to code in python. So I can just suggest that you can try to optimize your code.
Like even without sorting you can solve this question.

Try this test case:
1
aaabbbdddd
ddab

Your Output: aabbddddab
Correct Output : aabbddabdd

Also, print the answer to each test case in the newline.
You can check my solution : CodeChef: Practical coding for everyone