fork(3) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5.  
  6. long long int t, i = 0;
  7. long int alpha[26] = { 0 }, value, test = 0, flag = 0, count = 0, max = 0,
  8. j = 0, pos = 0, prevpos = 0;
  9. char str[100000];
  10.  
  11. scanf("%lld", &t);
  12. while (t--) {
  13.  
  14. for (i = 0; i < 25; i++)
  15. alpha[i] = 0;
  16.  
  17. flag = 0;
  18. scanf("%s", str);
  19. i = 0;
  20. while (str[i] != '\0') {
  21. value = str[i] - 97;
  22. //printf("%d", value);
  23. alpha[value]++;
  24. i++;
  25. }
  26.  
  27. count = i;
  28. if (i % 2 == 0)
  29. test = i / 2;
  30. else
  31. test = i / 2 + 1;
  32.  
  33. for (i = 0; i < 25; i++) {
  34. if (alpha[i] > test) {
  35. printf("-1\n");
  36. flag = 1;
  37. break;
  38.  
  39. }
  40. }
  41. if (flag == 0) {
  42.  
  43. prevpos = 100;
  44. pos = 0;
  45. while (count--) {
  46. max = 0;
  47. for (j = 0; j < 26; j++) {
  48. if (max < alpha[j] && prevpos != j) {
  49. max = alpha[j];
  50. pos = j;
  51. }
  52. }
  53. prevpos = pos;
  54. alpha[pos]--;
  55. printf("%c", pos + 97);
  56.  
  57. }
  58. printf("\n");
  59. }
  60.  
  61. }
  62. return 0;
  63. }
  64.  
  65.  
Success #stdin #stdout 0s 2032KB
stdin
1
aaabbbcccccc
stdout
cacbcacbcabc