Submission #448539

# Submission time Handle Problem Language Result Execution time Memory
448539 2021-07-30T13:14:15 Z rainboy Rima (COCI17_rima) C
28 / 140
709 ms 48276 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	500000
#define L	3000000

int max(int a, int b) { return a > b ? a : b; }

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

char *ss[N]; int ll[N];

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = strcmp(ss[ii[j]], ss[i_]);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;
	
	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

char visited[N]; int dp[N];

void dfs(int i) {
	int o;

	if (visited[i])
		return;
	visited[i] = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs(j);
		dp[i] = max(dp[i], dp[j] - 1);
	}
	dp[i] += 1 + eo[i];
}

int main() {
	static int ii[N];
	static char root[N];
	int n, i, j, ans;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		static char s[L + 1];
		int h1, h2;
		char tmp;

		scanf("%s", s), ss[i] = strdup(s), ll[i] = strlen(s);
		for (h1 = 0, h2 = ll[i] - 1; h1 < h2; h1++, h2--)
			tmp = ss[i][h1], ss[i][h1] = ss[i][h2], ss[i][h2] = tmp;
		ii[i] = i;
	}
	sort(ii, 0, n);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (i = 0, j = 0; j < n; j++) {
		int j_ = ii[j];
		char c = ss[j_][ll[j_] - 1];

		ss[j_][ll[j_] - 1] = 0;
		while (i < j && strcmp(ss[ii[i]], ss[j_]) < 0)
			i++;
		if (i < j && strcmp(ss[ii[i]], ss[j_]) == 0)
			append(ii[i], j_);
		else
			root[j_] = 1;
		ss[j_][ll[j_] - 1] = c;
	}
	for (i = 0; i < n; i++)
		if (root[i])
			dfs(i);
	for (i = 0; i < n; i++)
		ss[i][ll[i] - 1] = 0;
	ans = 0;
	for (i = 0; i < n; i = j) {
		int mx1, mx2;

		j = i, mx1 = mx2 = 0;
		while (j < n && strcmp(ss[ii[i]], ss[ii[j]]) == 0) {
			int j_ = ii[j++];

			if (mx1 < dp[j_] - 1)
				mx2 = mx1, mx1 = dp[j_] - 1;
			else if (mx2 < dp[j_] - 1)
				mx2 = dp[j_] - 1;
		}
		ans = max(ans, (root[ii[i]] ? 0 : 1) + j - i + mx1 + mx2);
	}
	printf("%d\n", ans);
	return 0;
}

Compilation message

rima.c: In function 'append':
rima.c:45:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   45 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
rima.c: In function 'main':
rima.c:72:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
rima.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%s", s), ss[i] = strdup(s), ll[i] = strlen(s);
      |   ^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Correct 0 ms 332 KB Output is correct
3 Incorrect 0 ms 332 KB Output isn't correct
4 Incorrect 709 ms 48276 KB Output isn't correct
5 Correct 26 ms 3396 KB Output is correct
6 Incorrect 8 ms 1432 KB Output isn't correct
7 Incorrect 6 ms 1320 KB Output isn't correct
8 Incorrect 5 ms 1228 KB Output isn't correct
9 Incorrect 47 ms 4924 KB Output isn't correct
10 Incorrect 5 ms 1228 KB Output isn't correct