Submission #220227

# Submission time Handle Problem Language Result Execution time Memory
220227 2020-04-07T11:32:33 Z kostia244 Palindromes (APIO14_palindrome) C++17
100 / 100
97 ms 95648 KB
#pragma GCC optimize("O2")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,sse,sse2,ssse3,tune=native")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
using namespace std;
using ll = long long;
//using arr = array;
using pi = pair<int, int>;
using vi = vector<int>;
using vpi = vector<pi>;
using ld = long double;
//#include<bits/extc++.h>
//using namespace __gnu_pbds;
//using oset = tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>;
const int maxn = 1<<19, mod = (119<<23)+1, lg = 19;
int link[maxn], to[maxn][26], len[maxn], occ[maxn], sz, cur;
string s;
void init() {
	sz = 2;
	cur = 0;
	memset(link, 0, sizeof link);
	memset(len, 0, sizeof len);
	memset(to, 0, sizeof to);
	len[0] = -1;
}
int get_link(int v) {
	while((int)s.size()-len[v]-2<0 || s[s.size()-len[v]-2]!=s.back()) v = link[v];
	return v;
}

void add(char c) {
	s += c, c -= 'a';
	cur = get_link(cur);
	if(!to[cur][c]) {
		to[cur][c] = sz;
		link[sz] = cur ? to[get_link(link[cur])][c] : 1;
		len[sz] = len[cur]+2;
		sz++;
	}
	cur = to[cur][c];
	occ[cur]++;
}
vi g[maxn];
ll ans = 0;
ll dfs(int v) {
	ll cnt = occ[v];
	for(auto i : g[v]) {
		cnt += dfs(i);
	}
	if(len[v] > 0) ans = max(len[v] * 1ll * cnt, ans);
	return cnt;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	string s;
	cin >> s;
	init();
	for(auto &i : s) add(i);
	for(int i = 1; i < sz; i++) g[link[i]].pb(i);
	dfs(0); 
	cout << ans << '\n';
}

Compilation message

palindrome.cpp: In function 'void add(char)':
palindrome.cpp:37:15: warning: array subscript has type 'char' [-Wchar-subscripts]
  if(!to[cur][c]) {
               ^
palindrome.cpp:38:12: warning: array subscript has type 'char' [-Wchar-subscripts]
   to[cur][c] = sz;
            ^
palindrome.cpp:39:45: warning: array subscript has type 'char' [-Wchar-subscripts]
   link[sz] = cur ? to[get_link(link[cur])][c] : 1;
                                             ^
palindrome.cpp:43:17: warning: array subscript has type 'char' [-Wchar-subscripts]
  cur = to[cur][c];
                 ^
# Verdict Execution time Memory Grader output
1 Correct 43 ms 70136 KB Output is correct
2 Correct 45 ms 70136 KB Output is correct
3 Correct 44 ms 70144 KB Output is correct
4 Correct 42 ms 70140 KB Output is correct
5 Correct 43 ms 70144 KB Output is correct
6 Correct 42 ms 70144 KB Output is correct
7 Correct 43 ms 70136 KB Output is correct
8 Correct 41 ms 70144 KB Output is correct
9 Correct 41 ms 70136 KB Output is correct
10 Correct 49 ms 70136 KB Output is correct
11 Correct 43 ms 70136 KB Output is correct
12 Correct 42 ms 70144 KB Output is correct
13 Correct 42 ms 70136 KB Output is correct
14 Correct 44 ms 70144 KB Output is correct
15 Correct 42 ms 70136 KB Output is correct
16 Correct 42 ms 70144 KB Output is correct
17 Correct 42 ms 70136 KB Output is correct
18 Correct 45 ms 70144 KB Output is correct
19 Correct 42 ms 70136 KB Output is correct
20 Correct 42 ms 70136 KB Output is correct
21 Correct 42 ms 70136 KB Output is correct
22 Correct 45 ms 70048 KB Output is correct
23 Correct 43 ms 70144 KB Output is correct
24 Correct 44 ms 70144 KB Output is correct
25 Correct 43 ms 70144 KB Output is correct
26 Correct 42 ms 70144 KB Output is correct
27 Correct 42 ms 70144 KB Output is correct
28 Correct 43 ms 70136 KB Output is correct
29 Correct 42 ms 70136 KB Output is correct
30 Correct 42 ms 70144 KB Output is correct
31 Correct 46 ms 70144 KB Output is correct
32 Correct 43 ms 70136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 70136 KB Output is correct
2 Correct 43 ms 70144 KB Output is correct
3 Correct 42 ms 70264 KB Output is correct
4 Correct 43 ms 70136 KB Output is correct
5 Correct 43 ms 70264 KB Output is correct
6 Correct 42 ms 70136 KB Output is correct
7 Correct 43 ms 70136 KB Output is correct
8 Correct 44 ms 70136 KB Output is correct
9 Correct 43 ms 70144 KB Output is correct
10 Correct 42 ms 70136 KB Output is correct
11 Correct 54 ms 70136 KB Output is correct
12 Correct 47 ms 70136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 70784 KB Output is correct
2 Correct 44 ms 70632 KB Output is correct
3 Correct 44 ms 71032 KB Output is correct
4 Correct 44 ms 70912 KB Output is correct
5 Correct 44 ms 70264 KB Output is correct
6 Correct 45 ms 70392 KB Output is correct
7 Correct 44 ms 70648 KB Output is correct
8 Correct 42 ms 70144 KB Output is correct
9 Correct 43 ms 70136 KB Output is correct
10 Correct 42 ms 70192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 76152 KB Output is correct
2 Correct 54 ms 75104 KB Output is correct
3 Correct 54 ms 78584 KB Output is correct
4 Correct 54 ms 76920 KB Output is correct
5 Correct 50 ms 71928 KB Output is correct
6 Correct 50 ms 72568 KB Output is correct
7 Correct 52 ms 73696 KB Output is correct
8 Correct 48 ms 70520 KB Output is correct
9 Correct 47 ms 72192 KB Output is correct
10 Correct 51 ms 71672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 79 ms 88352 KB Output is correct
2 Correct 76 ms 83616 KB Output is correct
3 Correct 80 ms 95648 KB Output is correct
4 Correct 76 ms 86688 KB Output is correct
5 Correct 97 ms 76700 KB Output is correct
6 Correct 70 ms 82208 KB Output is correct
7 Correct 68 ms 80544 KB Output is correct
8 Correct 53 ms 71412 KB Output is correct
9 Correct 51 ms 71424 KB Output is correct
10 Correct 76 ms 75552 KB Output is correct
11 Correct 77 ms 83872 KB Output is correct
12 Correct 53 ms 73244 KB Output is correct