/* MINIMIZING THE STRING
CHANDAN KUMAR
CSE */
#include<stdio.h>
#include<string.h>
#define s(x) scanf("%d",&x);
#define wh(t) while(t–)
int main(void) {
int test,len,i,j,pos,pos2,f,k,l,ppp,ds,ts;
char str[101],min;
s(test)
wh(test) {
s(len)
scanf("%s",str);
scanf("\n");
f=0;pos=100;
for(i=0;i<len-1;i++) {
if(f==1)
break;
min=str[i];
for(j=i+1;j<len;j++) {
if(min>=str[j]) {
min=str[j];
f=1;
pos=j;
}
}
}
pos2=100; f=0;
for(k=0;k<1;k++) {
if(f==1)
break;
for(l=k+1;l<len;l++) {
if(str[k]<str[l]) {
f=1;pos2=l;
break;
}
}
}
//printf(“k=%d\nl=%d\npos2=%d\n”,k,l,pos2);
k-=1;
char ss[100];
int m,p,g;
if(l!=len)
for(m=0;m<len;m++) {
if(m==k) {
f=1;
}
if(m+1==pos2 && f==1) {
ss[m]=str[k];
f=0;
}
else if(f==1)
ss[m]=str[m+1];
else
ss[m]=str[m];
}
else {
for(m=0;m<len-1;m++) {
ss[m]=str[m+1];
}
ss[m]=str[k];
}
/for(p=0;p<len;p++)
printf("%c\n",ss[p]);/
int pp=0; char sss[100];
//printf(“i=%d\nmin=%c\n,pos=%d\n”,i,min,pos);
if(i!=len-1) {
i-=1;
for(j=0;j<len;j++) {
if(j==i) {
//printf("%d\n",pp);
sss[pp++]=min;
}
// printf("%c",min);
else if(j==pos)
j++;
sss[pp++]=str[j];
//printf("%c",str[j]);
}
// printf("\n");
}
else {
strcpy(sss,str);
}
/for(p=0;p<len;p++)
printf("%c\n",sss[p]);/
ds=0;ts=0; int x;
for(x=0;x<len;x++) {
if(ss[x]>sss[x]){
ds=1;
break;
}
if(sss[x]>ss[x]) {
ts=1;
break;
}
}
if(ts==1) {
for(x=0;x<len;x++)
printf("%c",ss[x]);
printf("\n");
}
else if(ds==1) {
for(x=0;x<len;x++)
printf("%c",sss[x]);
printf("\n");
}
else if(x==len) {
for(x=0;x<len;x++)
printf("%c",sss[x]);
printf("\n");
}
/for(i=len-1;i>=1;i++) {
for(j=i-1;j>=0;j++) {
if(str[i])
}
}/
}
return 0;
}
post link of your solution…
Can you read what you posted? I cant!
Please post the link of your solution either the codechef one or the ideone one.
Also please take care that you do not repeat this again
1 Like
http://www.codechef.com/viewsolution/6568669
HERE IS MY CODE. CAN’T FIND THE PROBLEM. GIVE ME SOME TEST CASE FOR WHICH IT IS GIVING THE WRONG ANSWER.