cses

cses solutions in pure c
git clone git://git.superpozycja.net/cses
log | files | refs

s.c (844B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <stdint.h>
      5 
      6 int solve()
      7 {
      8 	uint32_t abc[26] = { 0 };
      9 	uint8_t odd_ix;
     10 	uint8_t odds;
     11 	uint32_t n;
     12 	int i, j;
     13 	char c;
     14 
     15 	n = 0;
     16 	odds = 0;
     17 	odd_ix = 0xff;
     18 
     19 	while ((c = getchar()) != EOF) {
     20 		abc[c - 0x41]++;
     21 		n++;
     22 	}
     23 	n--;
     24 
     25 	for (i = 0; i < 26; i++) {
     26 		odds += abc[i] % 2;
     27 		if (abc[i] % 2)
     28 			odd_ix = i;
     29 	}
     30 
     31 	if (odds != n % 2) {
     32 		printf("NO SOLUTION\n");
     33 		return -1;
     34 	}
     35 
     36 	for (i = 0; i < 26; i++) {
     37 		if (i == odd_ix)
     38 			continue;
     39 
     40 		for (j = 0; j < abc[i]/2; j++)
     41 			putchar(i + 0x41);
     42 	}
     43 
     44 	if (odds)
     45 		for (i = 0; i < abc[odd_ix]; i++)
     46 			putchar(odd_ix + 0x41);
     47 
     48 	for (i = 25; i >= 0; i--) {
     49 		if (i == odd_ix)
     50 			continue;
     51 
     52 		for (j = 0; j < abc[i]/2; j++)
     53 			putchar(i + 0x41);
     54 	}
     55 
     56 	putchar('\n');
     57 
     58 	return 0;
     59 }
     60 
     61 int main()
     62 {
     63 	int t = 1;
     64 	while (t--)
     65 		solve();
     66 }