#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], ans;
void dfs(int i) {
int o, mx1, mx2;
if (visited[i])
return;
visited[i] = 1;
mx1 = 0, mx2 = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
dfs(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, 1 + eo[i] + mx1 + mx2);
dp[i] = 1 + eo[i] + mx1;
}
int main() {
static int ii[N];
int n, i, j;
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_);
ss[j_][ll[j_] - 1] = c;
}
for (i = 0; i < n; i++)
dfs(i);
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:76:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
rima.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%s", s), ss[i] = strdup(s), ll[i] = strlen(s);
| ^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
605 ms |
47688 KB |
Output isn't correct |
5 |
Correct |
26 ms |
3276 KB |
Output is correct |
6 |
Incorrect |
7 ms |
1356 KB |
Output isn't correct |
7 |
Incorrect |
6 ms |
1228 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
1100 KB |
Output isn't correct |
9 |
Incorrect |
46 ms |
4656 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
1100 KB |
Output isn't correct |