Submission #77648

# Submission time Handle Problem Language Result Execution time Memory
77648 2018-09-29T09:40:55 Z samuelfgs96 Palindromes (APIO14_palindrome) C++11
100 / 100
156 ms 88452 KB
#include<bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

using namespace std;

const int N = 312345;
typedef pair<int, int> pii;
typedef long long ll;
 
struct treez {
	struct Node { 
		int start, end; 
		int length; 
		int insertEdg[26]; 
		int suffixEdg; 
		int qtd;
	}; 
	Node root1, root2; 
	Node tree[N]; 
	int currNode; 
	string s; 
	int ptr; 
	treez () { }
	treez (string s) : s(s) { 
		root1.length = -1; 
		root1.suffixEdg = 1; 
		root2.length = 0; 
		root2.suffixEdg = 1; 
		memset (tree, 0, sizeof tree);
		tree[1] = root1; 
		tree[2] = root2; 
		ptr = 2; 
		currNode = 1; 
		for (int i = 0; i < s.size(); i++) insert(i);
	}
	  
	void insert(int idx) { 
		int tmp = currNode;
		while (true) { 
			int curLength = tree[tmp].length; 
			if (idx - curLength >= 1 and s[idx] == s[idx-curLength-1]) 
				break; 
			tmp = tree[tmp].suffixEdg; 
		} 
	  
		if(tree[tmp].insertEdg[s[idx]-'a'] != 0) { 
			currNode = tree[tmp].insertEdg[s[idx]-'a']; 
			tree[currNode].qtd++;
			return; 
		} 
		ptr++; 
		tree[tmp].insertEdg[s[idx]-'a'] = ptr; 
		tree[ptr].length = tree[tmp].length + 2; 
		tree[ptr].end = idx; 
		tree[ptr].start = idx - tree[ptr].length + 1; 
		tree[ptr].qtd = 1;
	  
		tmp = tree[tmp].suffixEdg; 
		currNode = ptr; 
		if (tree[currNode].length == 1) { 
			tree[currNode].suffixEdg = 2; 
			return; 
		} 
		while (true) { 
			int curLength = tree[tmp].length; 
			if (idx-curLength >= 1 and s[idx] == s[idx-curLength-1]) 
				break; 
			tmp = tree[tmp].suffixEdg; 
		} 
	  
		tree[currNode].suffixEdg = tree[tmp].insertEdg[s[idx]-'a']; 
	} 
};
treez t1;
string s;
ll ans;
int sz[N];
void dfs (int u) {
	for (int i = t1.ptr; i >= 3; i--) {
		sz[i] += t1.tree[i].qtd;
		sz[t1.tree[i].suffixEdg] += sz[i];
	}
}
void dfs2 (int u) {
	for (int i = 0; i < 26; i++)
		if (t1.tree[u].insertEdg[i])
			dfs2(t1.tree[u].insertEdg[i]);
	if (u >= 3) {
		ans = max(ans, ll(sz[u]) * (t1.tree[u].end - t1.tree[u].start + 1));
	}

}
int main (void) {
	cin >> s;
	t1 = treez(s);
	dfs(1);
	dfs2(1);
	dfs2(2);
	cout << ans << endl;
	return 0;
}

Compilation message

palindrome.cpp: In constructor 'treez::treez(std::__cxx11::string)':
palindrome.cpp:39:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i < s.size(); i++) insert(i);
                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 70 ms 76280 KB Output is correct
2 Correct 70 ms 76308 KB Output is correct
3 Correct 68 ms 76308 KB Output is correct
4 Correct 69 ms 76412 KB Output is correct
5 Correct 71 ms 76412 KB Output is correct
6 Correct 69 ms 76412 KB Output is correct
7 Correct 69 ms 76412 KB Output is correct
8 Correct 79 ms 76412 KB Output is correct
9 Correct 68 ms 76412 KB Output is correct
10 Correct 66 ms 76412 KB Output is correct
11 Correct 70 ms 76412 KB Output is correct
12 Correct 68 ms 76412 KB Output is correct
13 Correct 68 ms 76412 KB Output is correct
14 Correct 68 ms 76412 KB Output is correct
15 Correct 74 ms 76412 KB Output is correct
16 Correct 70 ms 76412 KB Output is correct
17 Correct 77 ms 76412 KB Output is correct
18 Correct 71 ms 76412 KB Output is correct
19 Correct 69 ms 76412 KB Output is correct
20 Correct 70 ms 76412 KB Output is correct
21 Correct 70 ms 76412 KB Output is correct
22 Correct 73 ms 76412 KB Output is correct
23 Correct 72 ms 76412 KB Output is correct
24 Correct 71 ms 76412 KB Output is correct
25 Correct 71 ms 76412 KB Output is correct
26 Correct 68 ms 76412 KB Output is correct
27 Correct 71 ms 76412 KB Output is correct
28 Correct 70 ms 76412 KB Output is correct
29 Correct 69 ms 76412 KB Output is correct
30 Correct 68 ms 76416 KB Output is correct
31 Correct 69 ms 76416 KB Output is correct
32 Correct 70 ms 76416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 76416 KB Output is correct
2 Correct 68 ms 76416 KB Output is correct
3 Correct 72 ms 76416 KB Output is correct
4 Correct 68 ms 76416 KB Output is correct
5 Correct 73 ms 76416 KB Output is correct
6 Correct 70 ms 76416 KB Output is correct
7 Correct 71 ms 76416 KB Output is correct
8 Correct 74 ms 76416 KB Output is correct
9 Correct 70 ms 76416 KB Output is correct
10 Correct 70 ms 76416 KB Output is correct
11 Correct 69 ms 76416 KB Output is correct
12 Correct 70 ms 76416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 76668 KB Output is correct
2 Correct 72 ms 76668 KB Output is correct
3 Correct 71 ms 76668 KB Output is correct
4 Correct 71 ms 76668 KB Output is correct
5 Correct 77 ms 76668 KB Output is correct
6 Correct 70 ms 76668 KB Output is correct
7 Correct 78 ms 76668 KB Output is correct
8 Correct 69 ms 76668 KB Output is correct
9 Correct 73 ms 76668 KB Output is correct
10 Correct 71 ms 76668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 79344 KB Output is correct
2 Correct 87 ms 79344 KB Output is correct
3 Correct 85 ms 79356 KB Output is correct
4 Correct 83 ms 79356 KB Output is correct
5 Correct 83 ms 79356 KB Output is correct
6 Correct 82 ms 79356 KB Output is correct
7 Correct 82 ms 79356 KB Output is correct
8 Correct 75 ms 79356 KB Output is correct
9 Correct 80 ms 79356 KB Output is correct
10 Correct 91 ms 79356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 129 ms 85360 KB Output is correct
2 Correct 114 ms 85360 KB Output is correct
3 Correct 121 ms 86028 KB Output is correct
4 Correct 116 ms 86028 KB Output is correct
5 Correct 118 ms 86028 KB Output is correct
6 Correct 113 ms 86028 KB Output is correct
7 Correct 111 ms 86028 KB Output is correct
8 Correct 89 ms 86028 KB Output is correct
9 Correct 87 ms 86028 KB Output is correct
10 Correct 116 ms 87104 KB Output is correct
11 Correct 156 ms 88452 KB Output is correct
12 Correct 88 ms 88452 KB Output is correct