Submission #101381

# Submission time Handle Problem Language Result Execution time Memory
101381 2019-03-18T20:26:19 Z KCSC Palindromes (APIO14_palindrome) C++14
100 / 100
250 ms 71364 KB
#include <bits/stdc++.h>
using namespace std;

class Eertree {

private:

	struct Node {
		map<char, int> son; 
		int length, suffixLink, start, end, psm = 0;
		vector<int> lnk;

		Node(int _length = 0, int _start = 0, int _end = 0) :
			length(_length), suffixLink(0), start(_start), end(_end) {} 
	};

	int last;
	vector<Node> tree; 
	string myString;

	void _addLetter(char ch) {
		int idx = myString.length(), tmp = last;
		myString.push_back(ch);		
		while (true) {
			int len = tree[tmp].length;
			if (idx - len and myString[idx - len - 1] == myString[idx]) {
				break; }
			tmp = tree[tmp].suffixLink; }
		if (tree[tmp].son.find(ch) != tree[tmp].son.end()) {
			last = tree[tmp].son[ch];
			return; }
		tree.push_back(Node(tree[tmp].length + 2, idx - tree[tmp].length - 1, idx));
		last = tree[tmp].son[ch] = tree.size() - 1; tmp = tree[tmp].suffixLink;
		if (tree[last].length == 1) {
			tree[last].suffixLink = 1;
			tree[1].lnk.push_back(last);
			return; }
		while (true) {
			int len = tree[tmp].length;
			if (idx - len and myString[idx - len - 1] == myString[idx]) {
				break; }
			tmp = tree[tmp].suffixLink; }
		tree[last].suffixLink = tree[tmp].son[ch]; tree[tree[tmp].son[ch]].lnk.push_back(last);
		return; }

	void dfs(int x, long long &ans) {
		for (int y : tree[x].lnk) {
			dfs(y, ans); tree[x].psm += tree[y].psm; }
		if (x >= 2) {
			ans = max(ans, 1LL * (tree[x].end - tree[x].start + 1) * tree[x].psm); } }

public:

	Eertree() : last(0) {
		tree.push_back(Node(-1)); 
		tree.push_back(Node( 0)); }

	Eertree(const string str) : last(0) {
		tree.push_back(Node(-1));
		tree.push_back(Node( 0));
		for (char ch : str) {
			_addLetter(ch); } }

	void addLetter(const char ch) {
		_addLetter(ch); }

	void addString(const string str) {
 		for (char ch : str) {
			_addLetter(ch); } }

	void debug(const string str) {
		for (char ch : str) {
			_addLetter(ch); ++tree[last].psm; }
		long long ans = 0; dfs(0, ans); dfs(1, ans);
		cout << tree.size() - 2 << endl;
		for (int i = 2; i < tree.size(); ++i) {
			for (int j = tree[i].start; j <= tree[i].end; ++j) {
				cout << myString[j]; }
			cout << " " << tree[i].psm << endl; } }

	long long solve(const string str) {
		tree[0].lnk.push_back(1);
		for (char ch : str) {
			_addLetter(ch); ++tree[last].psm; }
		long long ans = 0; dfs(0, ans);
		return ans; }

} myEertree;

int main(void) {
//	freopen("palindrome.in", "r", stdin);
//	freopen("palindrome.out", "w", stdout);
	string str; cin >> str; 
	cout << myEertree.solve(str);
	return 0; }

Compilation message

palindrome.cpp: In member function 'void Eertree::debug(std::__cxx11::string)':
palindrome.cpp:76:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 2; i < tree.size(); ++i) {
                   ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 256 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
14 Correct 2 ms 256 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 256 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 512 KB Output is correct
9 Correct 3 ms 512 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2488 KB Output is correct
2 Correct 6 ms 2488 KB Output is correct
3 Correct 7 ms 2488 KB Output is correct
4 Correct 7 ms 2488 KB Output is correct
5 Correct 9 ms 2360 KB Output is correct
6 Correct 6 ms 2488 KB Output is correct
7 Correct 7 ms 2588 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 1464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 62 ms 19524 KB Output is correct
2 Correct 50 ms 18884 KB Output is correct
3 Correct 48 ms 21268 KB Output is correct
4 Correct 47 ms 19992 KB Output is correct
5 Correct 59 ms 17220 KB Output is correct
6 Correct 49 ms 17312 KB Output is correct
7 Correct 64 ms 17944 KB Output is correct
8 Correct 12 ms 896 KB Output is correct
9 Correct 20 ms 5424 KB Output is correct
10 Correct 46 ms 17052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 71248 KB Output is correct
2 Correct 185 ms 71360 KB Output is correct
3 Correct 170 ms 71364 KB Output is correct
4 Correct 154 ms 71248 KB Output is correct
5 Correct 193 ms 67292 KB Output is correct
6 Correct 176 ms 69908 KB Output is correct
7 Correct 122 ms 44628 KB Output is correct
8 Correct 26 ms 2304 KB Output is correct
9 Correct 27 ms 2324 KB Output is correct
10 Correct 129 ms 39824 KB Output is correct
11 Correct 250 ms 71248 KB Output is correct
12 Correct 35 ms 7276 KB Output is correct