DIVGOLD - Editorial

Someone please help.
Please someone give me a test case on which my solution fails.
Here is my code.
http://www.codechef.com/viewsolution/6574661

Anyone wanna help me find out counter example for my code to the problem?
http://www.codechef.com/viewsolution/6574368

i can be done in o(n).

lets take an example -abaaazzaa

alright now lets calculate frequency of all letters.(do it in ha table or something)
a-6,b-1,z-2

alright now , we store the leftmost a’s position that is 8(i=0 based index).
so its obvious that at index 1 , we need to replace b with a, which can be done in two ways.
1st way- we take the last a and inject it before b.
2nd way- we keep shifting b towards the end until there is a letter which is bigger than b.
the key obs is that , the answer has to be one of it , there is no other option, so what we do is buid both the string seperatly (both are o(n)).
now s1=aabaaazza and s2=aaaabzza
so the answer is s2.
if there is any cornor case im missing , pls lemme know :slight_smile:

@amitpandeykgp i solved this question using C++ stl ( insert and erase function).I just want to know whether complexity of insert and erase is O(1) or O(n)?? Here is my solution :
https://www.codechef.com/viewsolution/10649315

So, overall Complexity is O(n^3) or O(n^2) ??

all test cases given above are working then also i am getting a wrong ans
https://www.codechef.com/viewsolution/10858733

help!

can anyone provide me some test cases

i think my approach is o(n)
link:CodeChef: Practical coding for everyone
with two steps :
first insert at first mismatch
second: remove at first mismatch
and than compare both solution and print that which is lesser

It is wrong. Counter example: ABAA: In this case, you will print AABA, but you can make it AAAB too :slight_smile:

3 Likes

@dpraveen , No, my code prints AAAB only.

Link : rU6isc - Online C++ Compiler & Debugging Tool - Ideone.com

Test case : BAA.

Answer should be AAB.

But your code prints ABA.

2 Likes

From my explanation , s1 will be “ABAA” and s2 will be “AAAB”. And I’m taking the lexicaographically smaller of the two

What are you doing if there many characters which are equal to the smallest one?

What will be your output for “ZAZAZA”?

1 Like

In s1, Take the rightmost smallest character
In s2, take the rightmost greatest character

AZAZAZ

Test case : BAA.
Answer should be AAB.

Test case : ZAZA.
Answer should be AZAZ.

2 Likes

abaaza
s1 = aabaza
s2 = abaaaz
ans is aaabza :smiley:

4 Likes

I have answered to your question on forum. See there.

Thanks :slight_smile:
Should’ve given priority to the position for s2 :frowning:

Every language provides some fuctions to do such things. See http://www.cplusplus.com/reference/string/string/insert/ for C++.

1 Like