Codeforces Round #387 (Div. 2), problem: (E) Comments Solution in C/C++

Codeforces Round #387 (Div. 2), problem: (E) Comments Solution in C/C++

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N	1000000

int ccnt;

struct comment {
	char *s;
	int d, i;
} cc[N];

char *ss[N];

int parse(int d, int i) {
	int k = atoi(ss[i + 1]);

	cc[ccnt].s = strdup(ss[i]);
	cc[ccnt].d = d;
	cc[ccnt++].i = i;
	i += 2;
	while (k-- > 0)
		i = parse(d + 1, i);
	return i;
}

int compare(const void *a, const void *b) {
	struct comment *pa = (struct comment *) a;
	struct comment *pb = (struct comment *) b;

	return pa->d != pb->d ? pa->d - pb->d : pa->i - pb->i;
}

int main() {
	int k_, k, i, j, m;
	static char s[N + 1], *t;

	scanf("%s", s);
	t = strtok(s, ",");
	for (k_ = 0; t != NULL; ss[k_++] = strdup(t), t = strtok(NULL, ","))
		;
	k = k_ / 2;
	ccnt = 0;
	for (i = 0; i < k_; i = parse(0, i))
		;
	qsort(cc, k, sizeof(*cc), compare);
	m = cc[k - 1].d + 1;
	printf("%d\n", m);
	for (i = 0, j = 0; i < m; i++) {
		while (j < k && cc[j].d == i) {
			printf("%s ", cc[j].s);
			j++;
		}
		printf("\n");
	}
	return 0;
}

 

 

Why Pet Travel Gear...

📢 As an Amazon Associate, I earn from qualifying...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to...

7 Reasons This Coffee...

📢 As an Amazon Associate, I earn from qualifying...

Why Kitchen Organization Tools...

📢 As an Amazon Associate, I earn from qualifying...

Is Dorm Room Hacks...

📢 As an Amazon Associate, I earn from qualifying...

Stop Buying Cheap Alternatives...

📢 As an Amazon Associate, I earn from qualifying...

Why Pet Travel Gear Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.After putting the Pet Travel Gear through a proper 30-day real-world test, I can tell...

Mall Together-TENOKE

Simulation gaming enthusiasts have a fresh operational challenge to tackle with the arrival of Mall Together-TENOKE. This brand-new release delivers an immersive shopping mall...

7 Reasons This Coffee Bar Accessories Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Why Kitchen Organization Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Is Dorm Room Hacks Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

Stop Buying Cheap Alternatives This Camping Essentials Is the Real Deal

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Everything You Need to Know About Outdoor Patio Tech Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Bathroom Organization Hacks Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.I'll be honest — I was skeptical at first. My daily routine had a gap...

Why Electric Cleaning Tools Is Currently Breaking the Internet

📢 As an Amazon Associate, I earn from qualifying purchases.💰 Official Price: Check on Amazon🌟 Why Buy the Electric Cleaning Tools?The Electric Cleaning Tools...