Submission #448550

#TimeUsernameProblemLanguageResultExecution timeMemory
448550rainboyRima (COCI17_rima)C11
140 / 140
824 ms49108 KiB
#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]; int compare(int i, int j) { return ll[i] != ll[j] ? ll[i] - ll[j] : strcmp(ss[i], ss[j]); } 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 = compare(ii[j], 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; } int dp[N]; void dfs(int i) { int o; 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]; while (i < j && ll[ii[i]] < ll[j_] - 1 || ll[ii[i]] == ll[j_] - 1 && strncmp(ss[ii[i]], ss[j_], ll[j_] - 1) < 0) i++; if (i < j && ll[ii[i]] == ll[j_] - 1 && strncmp(ss[ii[i]], ss[j_], ll[j_] - 1) == 0) append(ii[i], j_); else root[j_] = 1; } for (i = 0; i < n; i++) if (root[i]) dfs(i); ans = 0; for (i = 0; i < n; i = j) { int mx1, mx2; j = i, mx1 = mx2 = 0; while (j < n && ll[ii[i]] == ll[ii[j]] && strncmp(ss[ii[i]], ss[ii[j]], ll[ii[i]] - 1) == 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 (stderr)

rima.c: In function 'append':
rima.c:49:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   49 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
rima.c: In function 'main':
rima.c:90:16: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   90 |   while (i < j && ll[ii[i]] < ll[j_] - 1 || ll[ii[i]] == ll[j_] - 1 && strncmp(ss[ii[i]], ss[j_], ll[j_] - 1) < 0)
      |          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
rima.c:73:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
rima.c:79:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |   scanf("%s", s), ss[i] = strdup(s), ll[i] = strlen(s);
      |   ^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...