SKMP - Editorial

See my code …CodeChef: Practical coding for everyone

No need to check all characters of pattern it is sufficient to check first element of pattern … Even after this you didn’t understand then check my Code :: ))

Try These Test Cases and try solving whats wrong, dont give up soon,take your time, if still you are not able to do let me know, i will help
INPUT :
6
zzzety
ze
acdeedaghfaee
eeaghfa
qwertyuioppppppoiuyt
tyuui
zaazyz
azy
jtewmpzqmscas
pqssc
zatgggg
gt

Compare your output with this
Correct OUTPUT :

tyzezz
acddeeaghfaee
eiooppppppqrttyuuiwy
aazyzz
aejmmpqssctwz
aggggtz

This code literally passed all test cases but still I got only 80/100. I request you from my core of heart to tell me why:

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int i,j,k,testcase; string s,p,p2;//p2 is final output
cin>>testcase;
for(i=0;i<testcase;i++)
{
	p2 = "";
	cin>>s>>p;
	if(p==s)
		cout<<p<<"\n";
	else
	{
		for(j=0;j<p.size();j++)
		{
			for(k=0;k<s.size();k++)
			{
				if(p.at(j)==s.at(k))
				{
					s.erase(s.begin()+k);break;
				}
				else
				{
					
				}
			}
		}
		sort(s.begin(), s.end());k=0;int l = 1;//cout<<s<<endl;
		while(s.size()!=0)
		{

				if(s.at(k) < p.at(k))
				{
					p2 = p2 + s.at(k);
					s.erase(s.begin()+k);
				}
				else if(s.at(k) == p.at(k))
				{
					l = 1;
					string str ="a"; str.at(0) = s.at(k);
					while(str.at(0) == p.at(l))
					{
						l+=1;
					}
					if(p.at(l) > str.at(0))
					{
						p2 = p2 + s.at(k);
						s.erase(s.begin()+k);
					}
					else
					{
						p2 = p2 + p + s;break;
					}
				}
				else
				{
					p2 = p2 + p + s;break;
				}

		}
		cout<<p2<<"\n";
	}
}
return 0;

}

I tried these test cases and i found something very wierd. When i ran all these test cases, the last 2 are giving wrong output (‘a’ is missing at front in both test cases). But when i ran these two test cases alone, the output is coming to be correct (along ‘a’ at the front). Can you tell me what’s the problem?

I am using map. Why is it givinh runtime error ?

#include
#include
#include

using namespace std;

int main(){

int t;
cin>>t;

while(t--){

  string s1 , s2 , ans = "";
  cin>>s1>>s2;

  map<char,int>m;

  for(int i=0;i<s1.length();i++){

    if(i< s2.length()){
        m[s2[i]]--;
    }

    m[s1[i]]++;
  }

  bool flag = true;

  for(int i=1;i<s2.length();i++){

    if(s2[i] < s2[i-1]){
        flag = false;
        break;
    }

    if(s2[i] > s2[i-1]){
        break;
    }
  }

  //cout<<flag<<endl;

  auto x = m.begin();


  if(flag == true){

        for(; x->first<=s2[0] ; x++){

            for(int i=0;i<x->second;i++){
            ans.push_back(x->first);
            //cout<<x->first<<" ";
            }
        }


  }

  else{

    for(; x->first<s2[0] ; x++){

            for(int i=0;i<x->second;i++){
            ans.push_back(x->first);
           // cout<<x->first<<" ";
            }
        }
  }

  ans = ans + s2;

  //cout<<x->first<<" "<<x->second<<endl;1

  //cout<<ans<<endl;

  for(;x != m.end();x++){

    for(int i=0;i<x->second;i++)
    ans.push_back(x->first);
    //cout<<x->first<<" ";
  }

  cout<<ans<<endl;


}
return 0;

}

int n=s.length();
int m=s.length();

it should be
int n=s.length();
int m=p.length();

1 Like

Sort the characters not contributing to P using count sort. Now P can be inserted in 2 positions which are returned by upper_bound/lower_bound for the first character of P. The ans is the minimum of the two strings formed.

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll t, n, m, k, q, x, y;
    string s;
    bool flag;

	cin >> t;
	while (t--) {
		cin >> s;
		string p;
		cin >> p;

		vi cnt(26, 0);
		for (auto i: s)	cnt[i - 'a']++;

		for (auto j: p) cnt[j - 'a']--;

		string new_s;
		FOR (i, 0, 26) {
			if (cnt[i])	new_s += string(cnt[i], i + 'a');
		}

		string new_s2 = new_s;

		auto it = lower_bound(new_s.begin(), new_s.end(), p[0]);
		auto it2 = upper_bound(new_s2.begin(), new_s2.end(), p[0]);


		new_s.insert(it, p.begin(), p.end());
		new_s2.insert(it2, p.begin(), p.end());

		cout << (new_s < new_s2 ? new_s : new_s2) << '\n';
	}

    return 0;
}

What’s wrong with my solution.
https://www.codechef.com/viewsolution/36802496

I cant believe how i didn’t notice that! Thankyou so much @sksama.

i cant believe how i didnt notice that. Thanks alot!

Guys, Can some one tell me what is the mistake in my code, it was always giving WA. I didnt understand where I was going wrong.

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

I copy pasted the code from CodeChef: Practical coding for everyone from my own submission

:stuck_out_tongue:

I passed above test case till show me wrong answer.
can you check it pls.
https://www.codechef.com/viewsolution/36874272

Try these testcases:
Sample input
3
ppqssstx
stx
ppqsastx
sa
ppqszstx
sz
Sample output
ppqssstx
ppqsastx
ppqssztx
The testcases are by @usernameharsh

Your solution is so similar with the one which one was out during the contest. :woman_shrugging:

Yes,got the mistake ,WA on second of these,Thank you…

1 Like

try:
for _ in range(int(input())):
s=input()
p=input()

    if len(s)==len(p):
        print(p)
        break
    
    list1=[]
    list2=[]

    list1.extend(s)
    list2.extend(p)
    
    for i in list2:
        list1.remove(i)
    
    lists=[]
    listl=[]
    
    for i in list1:
        if i==p[0]:
            for j in p:
                if i==j:
                    continue
                elif i>j:
                    listl.append(i)
                    break
                else:
                    lists.append(i)
                    break
        elif i<p[0]:
            lists.append(i)
        else:
            listl.append(i)
            
    lists.sort()
    listl.sort()
    
    print(str(''.join(lists)+p+''.join(listl)))

except:
pass

it is passing every test cases on my ide but giving wa on codechef. Anyone help figure out the error

Can anyone tell me whats’s wrong with this solution.

No man!.I did it myself.ok

1 Like