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;
}

 

 

Is Desk Productivity Tools...

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

The Outer Worlds 2...

Gamers and RPG enthusiasts have reason to celebrate as...

Why Travel Organization Gear...

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

Why Everyone Is Talking...

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

Why Everyone Is Talking...

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

Why Everyone Is Talking...

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

Is Desk Productivity Tools Worth Your Money Here Is Our Full Review

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

The Outer Worlds 2 v1.2.0.1-TENOKE

Gamers and RPG enthusiasts have reason to celebrate as Obsidian Entertainment's acclaimed franchise returns with new developments. The highly anticipated sequel to the award-winning...

Why Travel Organization Gear Is Currently Breaking the Internet

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

Why Everyone Is Talking About the Car Detailing Essentials

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

Why Everyone Is Talking About the Smart Home Hacks

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

Why Everyone Is Talking About the Viral Kitchen Gadgets

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

Why Everyone Is Talking About the Travel Organization Gear

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

Why Everyone Is Talking About the Travel Organization Gear

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

GLP-1 Drugs Like Ozempic Carry Elevated Risks of Muscle Loss and Malnutrition in Adults Over 65

Understanding the Risks of GLP-1 Drugs for Older Adults GLP-1 receptor agonists, such as Ozempic, Wegovy, and Mounjaro, have transformed the landscape of weight management...