DIVGOLD - Editorial

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

1 Like

\mathcal{O}(n^2) algorithm
Let us consider the first position where the string s and sorted string has a mismatch.
There are two possible cases now.

  • Bring some character to this position.
  • Take character from this position to some other position

You can implement both the cases in \mathcal{O}(n^2) easily.

I have not given a proper thought, but a \mathcal{O}(n) can be made along the same lines.

I found the minimum position where the character was different from character at the same position when string was sorted. I brought that character from the furthest position in the string.
Ex:
katrina
I brought a from last to make it akatrin.

AAB
I didn’t do anything :stuck_out_tongue:

ABA
I brought A from last before B to make it AAB. Anything wrong you can think of with this?

yes, its incorrect , for example ABAA , ur answer is AABA bt answer is AAAB

What will you do in “AAABAAA”?

o(nlogn).
we will sort the list in ascending order.
then we will check for the first character which does nt match with the sorted list from the beginning of the given list.we will search this character from the end of the given list and bring it to correct position as in the sorted list

Your explanation doesn’t consider the case of inserting jth character at the beginning of the string.

I don’t know what happened to me last night not to solve this.
Now I see my mistake the inner for loop should be 0<= j < n

This wont work for the following case OHH

hey what about this approach get the ascending order of given string compare the sorted string one by one and then the first character that differs in given string then that of sorted one is inserted in and we remove the last occurence of that character.
please suggest if any correction my code is this http://www.codechef.com/viewsolution/6570974 but giving wrong answer

I solved it with O(N^2) per test case during the contest itself.

Here is my solution link :slight_smile:
http://www.codechef.com/viewsolution/6562897

1 Like

@dpraveen I tried exactly what u said… Correct me if i am wrong.
http://www.codechef.com/viewsolution/6570234

Here is my code . I tried an algorithm similar to selection but all I got was WA. I would be glad if anyone can help me in debugging

To insert character use str.insert() and REMEMBER to erase that character before inserting.Use str.erase().Also when using str.erase please use the position parameter as well as iterator parameter(here it is 1).

@dpraveen

I tried the same approach, getting WA.
Here: http://www.codechef.com/viewsolution/6567816

O(n*n)
Take every character and assume rest is sorted and insert this char in an appropriate position,lexicographically compare with the given string and then find out the minimum one

Code in Python : http://www.codechef.com/viewsolution/6564893

hi @dpraveen could you please tell me where am i going wrong

Still not able to figure out why my answer is wrong. Please provide some test cases.

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	//cout<<strcmp("fff","ddd");
	int t,i,j,k,len;
	char str[52],temp[52],chr,min[52];
	cin>>t;
	while(t--)
	{
		cin>>len>>str;
		strcpy(min,str);
		for(i=0;i<len;i++)
		{
			chr=str[i];
			for(j=0;j<len;j++)
			{
				strcpy(temp,str);
				if(i==j)
					continue;
				else if(i<j)
				{
					for(k=i;k<j;k++)
					{
						temp[k]=str[k+1];
					}
					temp[k]=chr;
				}
				else
				{
					for(k=i;k>j;k--)
					{
						temp[k]=str[k-1];
					}
					temp[k]=chr;
				}
				//cout<<temp<<endl;
				if(strcmp(temp,min)==-1)
					strcpy(min,temp);1
			}
		}
		cout<<min<<endl;
	}
	return 0;
}




nice editorial… thanks for posting

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