Submission #486531

# Submission time Handle Problem Language Result Execution time Memory
486531 2021-11-11T22:10:25 Z rainboy Palinilap (COI16_palinilap) C
54 / 100
1000 ms 24080 KB
#include <stdio.h>
#include <string.h>

#define N	100000
#define A	26

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

void manacher(char *cc, int *rr, int n) {
	static char cc_[N * 2 + 1];
	static int rr_[N * 2 + 1];
	int i, o, x;

	for (i = 0; i < n * 2 + 1; i++)
		cc_[i] = i % 2 == 1 ? cc[i / 2] : ' ';
	for (i = 0, o = 0, x = 0; i < n * 2 + 1; i++)
		if (i + rr_[o + o - i] < x)
			rr_[i] = rr_[o + o - i];
		else {
			o = i, x = max(x, i);
			while (x >= 0 && x < n * 2 + 1 && cc_[x] == cc_[o + o - x])
				x++;
			rr_[i] = x - o;
		}
	for (i = 0; i <= n - 1 + n - 1; i++)
		rr[i] = rr_[i + 1] / 2;
}

int main() {
	static long long kk[N + 1][A], ll[N + 1], mm[N + 1];
	static int rr[N + N - 1];
	static char cc[N + 1];
	int n, h, i, j, i_, j_, ij, a;
	long long k_;

	scanf("%s", cc), n = strlen(cc);
	manacher(cc, rr, n);
	for (ij = 0; ij <= n - 1 + n - 1; ij++) {
		i_ = ij / 2 - rr[ij], j_ = ij - i_;
		for (i = ij / 2, j = ij - i; i > i_; i--, j++) {
			if (ij % 2 == 0) {
				h = ij / 2;
				mm[i]++, mm[h]--, ll[h]++, ll[h + 1]--, mm[h + 1]++, mm[j + 1]--;
			} else
				mm[i]++, mm[j + 1]--;
			ll[0]++, ll[i]--, ll[j + 1]++, ll[n]--;
		}
		for ( ; i >= 0 && j < n; i--, j++) {
			if (i < i_ && cc[i] != cc[j])
				break;
			kk[i_][cc[j_] - 'a']++, kk[j_][cc[i_] - 'a']++;
		}
	}
	for (i = 1; i < n; i++)
		ll[i] += ll[i - 1];
	for (i = 1; i < n; i++)
		mm[i] += mm[i - 1];
	k_ = 0;
	for (i = 0; i < n; i++)
		for (a = 0; a < A; a++)
			k_ = max(k_, kk[i][a] + ll[i] + (cc[i] == a + 'a' ? mm[i] : 0));
	printf("%lld\n", k_);
	return 0;
}

Compilation message

palinilap.c: In function 'main':
palinilap.c:36:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |  scanf("%s", cc), n = strlen(cc);
      |  ^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 284 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 1432 KB Output is correct
2 Correct 36 ms 1512 KB Output is correct
3 Correct 2 ms 1484 KB Output is correct
4 Correct 1 ms 928 KB Output is correct
5 Correct 1 ms 1228 KB Output is correct
6 Correct 1 ms 1484 KB Output is correct
7 Correct 1 ms 1484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 24080 KB Output is correct
2 Execution timed out 1086 ms 10636 KB Time limit exceeded
3 Halted 0 ms 0 KB -