1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_MRO 300000
#define GO_LEFT  'L'
#define GO_RIGHT 'P'

size_t len;
char mro_0[MAX_MRO];
char mro_1[MAX_MRO];
char *mro = mro_0;
char *next = mro_1;
int ping[MAX_MRO];

const char *test = "LPPLPL";
const char* n = "6";

int main(int argc, char **argv)
{
	size_t i, j;
	len = atol((argc > 1) ? argv[1] : n);
	if (len > MAX_MRO)
		return -1;
	memcpy(mro, (argc > 2) ? argv[2] : test, sizeof(mro));
	memset(ping, 0, sizeof(ping));
	i = 0;
	j = len - 1;
	do {
		if (mro[i] == GO_LEFT) i++;
		if (mro[j] == GO_RIGHT) j--;
		for (size_t p = i; p <= j; p++)
		{
			if (mro[p] != mro[p + 1])
			{
				ping[p]++;

				next[p] = (mro[p] == GO_RIGHT) ? GO_LEFT : GO_RIGHT;
			} else {
				next[p] = mro[p];
			}
		}
		char* t = mro;
		mro = next;
		next = t;
	} while (i < j);

	for (size_t p = 0; p < len; p++)
		printf("%d ", ping[p]);
	printf("\r\n");

	return 0;
}