Submission #77254903


Source Code Expand

Copy
#include <stdio.h>
#include <stdlib.h>
struct meow {
char c;
int cnt;
};
int cmp(const void* x, const void* y) {
struct meow a = *(const struct meow*)x, b = *(const struct meow*)y;
return
((a.cnt < b.cnt) - (a.cnt > b.cnt)) * 2 +
((a.c > b.c) - (a.c < b.c));
}
int main(void) {
int T, tc;
char buf[32];
if (fgets(buf, sizeof(buf), stdin) == NULL || sscanf(buf, "%d", &T) != 1) return 1;
for (tc = 0; tc < T; tc++) {
struct meow cnt[26];
int c;
int i;
int cnt_all;
for (i = 0; i < 26; i++) cnt[i] = (struct meow){ 'a' + i, 0 };
while ((c = getchar()) != '\n' && c != EOF) {
int id = c - 'a';
if (0 <= id && id < 26) cnt[id].cnt++;
}
qsort(cnt, 26, sizeof(*cnt), cmp);
for (i = 0, cnt_all = 0; i < 26; i++) cnt_all += cnt[i].cnt;
if (cnt[0].cnt > (cnt_all + 1) / 2) {
puts("No");
} else {
char* res = malloc(cnt_all + 1);
int p;
if (res == NULL) return 2;
puts("Yes");
for (i = 0, p = 0; i < 26; i++) {
int j;
for (j = 0; j < cnt[i].cnt; j++) {
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>

struct meow {
	char c;
	int cnt;
};

int cmp(const void* x, const void* y) {
	struct meow a = *(const struct meow*)x, b = *(const struct meow*)y;
	return
		((a.cnt < b.cnt) - (a.cnt > b.cnt)) * 2 +
		((a.c > b.c) - (a.c < b.c));
}

int main(void) {
	int T, tc;
	char buf[32];
	if (fgets(buf, sizeof(buf), stdin) == NULL || sscanf(buf, "%d", &T) != 1) return 1;
	for (tc = 0; tc < T; tc++) {
		struct meow cnt[26];
		int c;
		int i;
		int cnt_all;
		for (i = 0; i < 26; i++) cnt[i] = (struct meow){ 'a' + i, 0 };
		while ((c = getchar()) != '\n' && c != EOF) {
			int id = c - 'a';
			if (0 <= id && id < 26) cnt[id].cnt++;
		}
		qsort(cnt, 26, sizeof(*cnt), cmp);
		for (i = 0, cnt_all = 0; i < 26; i++) cnt_all += cnt[i].cnt;
		if (cnt[0].cnt > (cnt_all + 1) / 2) {
			puts("No");
		} else {
			char* res = malloc(cnt_all + 1);
			int p;
			if (res == NULL) return 2;
			puts("Yes");
			for (i = 0, p = 0; i < 26; i++) {
				int j;
				for (j = 0; j < cnt[i].cnt; j++) {
					res[p] = cnt[i].c;
					p += 2;
					if (p >= cnt_all) p = 1;
				}
			}
			res[cnt_all] = '\0';
			puts(res);
			free(res);
		}
	}
	return 0;
}

/*

oxoxoxoxo

oxoxoxoxox

ある文字が全体の半分 (切り上げ) を超える → No
otherwise → Yes (多い順に一つおきに流し込む)

*/

Submission Info

Submission Time
Task D - Adjacent Distinct String
User mikecat
Language C23 (GCC 14.2.0)
Score 400
Code Size 1384 Byte
Status AC
Exec Time 109 ms
Memory 2736 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 1
AC × 21
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 1580 KiB
01_handmade_00.txt AC 109 ms 2736 KiB
01_handmade_01.txt AC 40 ms 1612 KiB
01_handmade_02.txt AC 4 ms 1580 KiB
01_handmade_03.txt AC 6 ms 2632 KiB
01_handmade_04.txt AC 6 ms 2604 KiB
01_handmade_05.txt AC 8 ms 1580 KiB
02_random_00.txt AC 6 ms 2524 KiB
02_random_01.txt AC 6 ms 2504 KiB
02_random_02.txt AC 6 ms 2604 KiB
02_random_03.txt AC 4 ms 1612 KiB
02_random_04.txt AC 4 ms 1476 KiB
02_random_05.txt AC 4 ms 1580 KiB
02_random_06.txt AC 5 ms 1616 KiB
02_random_07.txt AC 6 ms 2476 KiB
02_random_08.txt AC 10 ms 1624 KiB
02_random_09.txt AC 8 ms 1624 KiB
02_random_10.txt AC 6 ms 1624 KiB
02_random_11.txt AC 5 ms 1632 KiB
02_random_12.txt AC 5 ms 1612 KiB
02_random_13.txt AC 5 ms 1580 KiB


2026-07-07 (Tue)
07:54:45 +09:00