fork(5) download
  1. #include <stdio.h>
  2. #include<bits/stdc++.h>
  3. void srt(int x[],int first,int last,char c[])
  4. {
  5. int i,j,temp;
  6. char temp1;
  7. for(i=0;i<=last;i++)
  8. {
  9. for(j=i;j<=last;j++)
  10. {
  11. if(x[i]>x[j])
  12. {
  13. temp=x[i];
  14. x[i]=x[j];
  15. x[j]=temp;
  16. temp1=c[i];
  17. c[i]=c[j];
  18. c[j]=temp1;
  19. }
  20. }
  21. }
  22. }
  23. int cal(char str[],int g)
  24. {
  25.  
  26. int s[27]={0},i,j;
  27. int f1=0;
  28. for(i=0;i<g-1;i++)
  29. {
  30. if(str[i]==str[i+1])
  31. {
  32. f1=1;
  33. break;
  34. }
  35. }
  36. if(f1==1)
  37. {
  38. for(i=0;i<g;i++)
  39. {
  40. s[str[i]-'a']++;
  41.  
  42. }
  43. char a1[26];
  44. int a2[26];
  45.  
  46. int u=0,h=0;
  47. for(i=0;i<26;i++)
  48. {
  49. if(s[i]!=0)
  50. {
  51. a1[h]=97+i;
  52. a2[h]=s[i];
  53. h++;
  54. }
  55. }
  56. char str2[g];
  57. int f=0;
  58. while(f<g)
  59. {
  60. srt(a2,0,h-1,a1);
  61. if(a2[h-1]>0)
  62. {
  63. str2[u++]=a1[h-1];
  64. a2[h-1]--;
  65. f++;
  66. }
  67. if(a2[h-2]>0)
  68. {
  69. str2[u++]=a1[h-2];
  70. a2[h-2]--;
  71. f++;
  72. }
  73. }
  74.  
  75. str2[f]='\0';
  76.  
  77. f1=0;
  78. for(i=0;i<g-1;i++)
  79. {
  80. if(str2[i]==str2[i+1])
  81. {
  82. printf("-1\n");
  83. f1=1;
  84. break;
  85. }
  86. }
  87. if(f1==0)
  88. printf("%s\n",str2);
  89. }
  90. else
  91. printf("%s\n",str);
  92. }
  93. int main(void)
  94. {
  95. int t;
  96. scanf("%d",&t);
  97. while(t--)
  98. {
  99. int i,j,n;
  100. char str[100001];
  101.  
  102. scanf("%s",str);
  103. int g=strlen(str);
  104. cal(str,g);
  105.  
  106. }
  107. return 0;
  108. }
Success #stdin #stdout 0s 3464KB
stdin
1
abbc
stdout
bcba