fork download
  1. /* MINIMIZING THE STRING
  2. CHANDAN KUMAR
  3. CSE */
  4. #include<stdio.h>
  5. #include<string.h>
  6. #define s(x) scanf("%d",&x);
  7. #define wh(t) while(t--)
  8. int main(void) {
  9. int test,len,i,j,pos,pos2,f,k,l,ppp,ds,ts;
  10. char str[101],min;
  11. s(test)
  12. wh(test) {
  13. s(len)
  14. scanf("%s",str);
  15. f=0;pos=100;
  16. for(i=0;i<len-1;i++) {
  17. if(f==1)
  18. break;
  19. min=str[i];
  20. for(j=i+1;j<len;j++) {
  21. if(min>=str[j]) {
  22. min=str[j];
  23. f=1;
  24. pos=j;
  25. }
  26. }
  27. }
  28. pos2=100; f=0;
  29. for(k=0;k<len-1;k++) {
  30. if(f==1)
  31. break;
  32. for(l=k+1;l<len;l++) {
  33. if(str[k]<str[l]) {
  34. f=1;pos2=l;
  35. break;
  36. }
  37. }
  38. }
  39. //printf("k=%d\nl=%d\npos2=%d\n",k,l,pos2);
  40. k-=1;
  41. char ss[100];
  42. int m,p,g;
  43. if(l!=len)
  44. for(m=0;m<len;m++) {
  45. if(m==k) {
  46. f=1;
  47. }
  48. if(m+1==pos2 && f==1) {
  49. ss[m]=str[k];
  50. f=0;
  51. }
  52. else if(f==1)
  53. ss[m]=str[m+1];
  54. else
  55. ss[m]=str[m];
  56. }
  57. else {
  58. for(m=0;m<len-1;m++) {
  59. ss[m]=str[m+1];
  60. }
  61. ss[m]=str[k];
  62. }
  63. /*for(p=0;p<len;p++)
  64. printf("%c\n",ss[p]);*/
  65. int pp=0; char sss[100];
  66. //printf("i=%d\nmin=%c\n,pos=%d\n",i,min,pos);
  67. if(i!=len-1) {
  68. i-=1;
  69. for(j=0;j<len;j++) {
  70. if(j==i) {
  71. //printf("%d\n",pp);
  72. sss[pp++]=min;
  73. }
  74. // printf("%c",min);
  75. else if(j==pos)
  76. j++;
  77. sss[pp++]=str[j];
  78. //printf("%c",str[j]);
  79. }
  80. // printf("\n");
  81. }
  82. else {
  83. strcpy(sss,str);
  84. }
  85. /*for(p=0;p<len;p++)
  86. printf("%c\n",sss[p]);*/
  87. ds=0;ts=0; int x;
  88. for(x=0;x<len;x++) {
  89. if(ss[x]>sss[x]){
  90. ds=1;
  91. break;
  92. }
  93. if(sss[x]>ss[x]) {
  94. ts=1;
  95. break;
  96. }
  97. }
  98. if(ts==1) {
  99. for(x=0;x<len;x++)
  100. printf("%c",ss[x]);
  101. printf("\n");
  102. }
  103. else if(ds==1) {
  104. for(x=0;x<len;x++)
  105. printf("%c",sss[x]);
  106. printf("\n");
  107. }
  108. else if(x==len) {
  109. for(x=0;x<len;x++)
  110. printf("%c",sss[x]);
  111. printf("\n");
  112. }
  113. /*for(i=len-1;i>=1;i++) {
  114. for(j=i-1;j>=0;j++) {
  115. if(str[i])
  116. }
  117. }*/
  118. }
  119. return 0;
  120. }
Success #stdin #stdout 0s 2012KB
stdin
5
4
DCBA
7
XYZZYZZ
4
AABA
6
ZAZAZA
3
BBA
stdout
ADCB
XYYZZZZ
AAAB
AAZAZA
ABB