Submission #223920

# Submission time Handle Problem Language Result Execution time Memory
223920 2020-04-16T20:52:47 Z lucas110550 Palindromes (APIO14_palindrome) C++14
100 / 100
71 ms 65272 KB
#include <bits/stdc++.h>

using namespace std;

char str[300005];
int n;
int len[300005], ch[300005][26];
int r[300005], f[300005];
char s[101];
int h, t, cnt;
long long sum = 0;
int st, ed;
vector<int> E[300005];

void init() {
	for (int i = 0; i < 2; i ++) memset(ch[i], 0, sizeof(ch[i]));
	len[0] = 0; len[1] = -1;
	f[0] = 1; f[1] = 1;
	st = ed = 0;
	cnt = 1;
	sum = 0;
}

bool valid(int x) {
	return 1 <= x && x <= t;
}

void front_ins(int x) {
	while (!valid(x + len[st] + 1) || str[x + len[st] + 1] != str[x]) st = f[st];
	if (!ch[st][str[x] - 'a']) {
		int j = f[st];
		while (!valid(x + len[j] + 1) || str[x + len[j] + 1] != str[x]) j = f[j];
		len[++ cnt] = len[st] + 2;
		int ff = ch[j][str[x] - 'a'];
		f[cnt] = ff;
		E[ff].push_back(cnt);
		ch[st][str[x] - 'a'] = cnt;
		memset(ch[cnt], 0, sizeof(ch[cnt]));
	}
	st = ch[st][str[x] - 'a'];
	r[st] ++;
//	sum += r[st];
	if (len[st] == t - h) ed = st;
}

void back_ins(int x) {
	while (!valid(x - len[ed] - 1) || str[x - len[ed] - 1] != str[x]) ed = f[ed];
	if (!ch[ed][str[x] - 'a']) {
		int j = f[ed];
		while (!valid(x - len[j] - 1) || str[x - len[j] - 1] != str[x]) j = f[j];
		len[++ cnt] = len[ed] + 2;
		int ff = ch[j][str[x] - 'a'];
		f[cnt] = ff;
		E[ff].push_back(cnt);
		ch[ed][str[x] - 'a'] = cnt;
		memset(ch[cnt], 0, sizeof(ch[cnt]));
	}
	ed = ch[ed][str[x] - 'a'];
	r[ed] ++;
//	sum += r[ed];
	if (len[ed] == t - h) st = ed;
}

void dfs(int x) {
	for (int i = 0; i < (int )E[x].size(); i ++) { 
		dfs(E[x][i]);
		r[x] += r[E[x][i]];
	}
}

int main( ) {
	init();
	scanf("%s", str + 1);
	n = (int )strlen(str + 1);
	for (int i = 1; i <= n; i ++) {
		++ t;
		back_ins(i);
	}
	dfs(0);
	for (int i = 2; i <= cnt; i ++) {
		sum = max(sum, 1LL * r[i] * len[i]);
	}
	printf("%lld\n", sum);
	return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", str + 1);
  ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 8 ms 7424 KB Output is correct
3 Correct 8 ms 7424 KB Output is correct
4 Correct 8 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 9 ms 7424 KB Output is correct
7 Correct 9 ms 7424 KB Output is correct
8 Correct 8 ms 7424 KB Output is correct
9 Correct 8 ms 7424 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 8 ms 7424 KB Output is correct
12 Correct 9 ms 7424 KB Output is correct
13 Correct 10 ms 7424 KB Output is correct
14 Correct 8 ms 7424 KB Output is correct
15 Correct 10 ms 7424 KB Output is correct
16 Correct 9 ms 7400 KB Output is correct
17 Correct 10 ms 7552 KB Output is correct
18 Correct 9 ms 7424 KB Output is correct
19 Correct 9 ms 7424 KB Output is correct
20 Correct 9 ms 7424 KB Output is correct
21 Correct 10 ms 7424 KB Output is correct
22 Correct 9 ms 7424 KB Output is correct
23 Correct 10 ms 7456 KB Output is correct
24 Correct 9 ms 7424 KB Output is correct
25 Correct 9 ms 7424 KB Output is correct
26 Correct 9 ms 7424 KB Output is correct
27 Correct 10 ms 7424 KB Output is correct
28 Correct 9 ms 7424 KB Output is correct
29 Correct 9 ms 7424 KB Output is correct
30 Correct 9 ms 7424 KB Output is correct
31 Correct 9 ms 7424 KB Output is correct
32 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 9 ms 7552 KB Output is correct
3 Correct 9 ms 7552 KB Output is correct
4 Correct 10 ms 7552 KB Output is correct
5 Correct 9 ms 7552 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 8 ms 7552 KB Output is correct
8 Correct 10 ms 7552 KB Output is correct
9 Correct 9 ms 7552 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 9 ms 7424 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9088 KB Output is correct
2 Correct 12 ms 8960 KB Output is correct
3 Correct 12 ms 9344 KB Output is correct
4 Correct 11 ms 9248 KB Output is correct
5 Correct 12 ms 8680 KB Output is correct
6 Correct 10 ms 8832 KB Output is correct
7 Correct 10 ms 8960 KB Output is correct
8 Correct 10 ms 7424 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 10 ms 8320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 24320 KB Output is correct
2 Correct 24 ms 23296 KB Output is correct
3 Correct 30 ms 26744 KB Output is correct
4 Correct 24 ms 24936 KB Output is correct
5 Correct 25 ms 19968 KB Output is correct
6 Correct 20 ms 17664 KB Output is correct
7 Correct 26 ms 20096 KB Output is correct
8 Correct 11 ms 7552 KB Output is correct
9 Correct 16 ms 12032 KB Output is correct
10 Correct 21 ms 18176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 58104 KB Output is correct
2 Correct 52 ms 53112 KB Output is correct
3 Correct 61 ms 65272 KB Output is correct
4 Correct 56 ms 56184 KB Output is correct
5 Correct 71 ms 46116 KB Output is correct
6 Correct 54 ms 47996 KB Output is correct
7 Correct 48 ms 44512 KB Output is correct
8 Correct 16 ms 7808 KB Output is correct
9 Correct 18 ms 8192 KB Output is correct
10 Correct 58 ms 39544 KB Output is correct
11 Correct 59 ms 53800 KB Output is correct
12 Correct 20 ms 13440 KB Output is correct