답안 #446966

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
446966 2021-07-24T05:50:13 Z BERNARB01 Palinilap (COI16_palinilap) C++17
17 / 100
1000 ms 13168 KB
#include <bits/stdc++.h>

using namespace std;

template<const int A>
class eertree {
private:
	int n, id, cr;
	vector<array<int, A>> T;
	vector<int> P, L, C;
	int Pr(const string& s, int i, int v) {
		while (i - L[v] - 1 < 0 || s[i - L[v] - 1] != s[i]) {
			v = P[v];
		}
		return v;
	}
	void add(const string& s, int i) {
		int ch = s[i] - 'a';
		cr = Pr(s, i, cr);
		if (T[cr][ch] == 0) {
			L[id] = 2 + L[cr];
			P[id] = T[Pr(s, i, P[cr])][ch];
			T[cr][ch] = id++;
		}
		cr = T[cr][ch];
		C[cr]++;
	}
public:
	eertree() {};
	eertree(const string& s) {
		n = s.length();
		T.assign(n + 2, {});
		P.assign(n + 2, 0);
		L.assign(n + 2, 0);
		C.assign(n + 2, 0);
		P[0] = 1;
		L[1] = -1;
		id = 2;
		cr = 0;
		for (int i = 0; i < n; i++) {
			add(s, i);
		}
	}
	int cnt_pals() {
		vector<int> inDeg(n + 2, 0);
		for (int i = 2; i < n + 2; i++) {
			inDeg[P[i]]++;
		}
		vector<int> que;
		for (int i = 2; i < n + 2; i++) {
			if (inDeg[i] == 0) {
				que.push_back(i);
			}
		}
		for (int b = 0; b < (int) que.size(); b++) {
			int u = que[b];
			int v = P[u];
			if (v < 2) {
				continue;
			}
			C[v] += C[u];
			inDeg[v]--;
			if (inDeg[v] == 0) {
				que.push_back(v);
			}
		}
		return accumulate(C.begin(), C.end(), 0);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	string s;
	cin >> s;
	int n = s.length(), ans = 0;
	for (int i = 0; i < n; i++) {
		char c = s[i];
		char& nc = s[i];
		for (nc = 'a'; nc <= 'z'; nc++) {
			ans = max(ans, eertree<26>(s).cnt_pals());
		}
		nc = c;
	}
	cout << ans << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 312 KB Output is correct
2 Correct 14 ms 328 KB Output is correct
3 Correct 14 ms 328 KB Output is correct
4 Correct 13 ms 204 KB Output is correct
5 Correct 12 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1074 ms 972 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1086 ms 13168 KB Time limit exceeded
2 Halted 0 ms 0 KB -