이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
Solution for APIO 2014 - Palindrome
Tags : palindromic tree
*/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("Ofast")
#pragma GCC optimization ("unroll-loops")
typedef long long ll;
typedef vector<int> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define fi first
#define se second
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
ll add(ll a, ll b){
return (a+b) % MOD;
}
ll mul(ll a, ll b){
a %= MOD;
return (a*b) % MOD;
}
ll gcd(ll a, ll b){
if (a == 1) return 1;
if (a == 0) return b;
return gcd(b%a, a);
}
string s;
const int N = 300001;
struct Node{
int len;
int nxt[26];
vi edges;
int sufflink;
int cnt;
//len = length of palindrome
//nxt[i] = palindrome formed by adding ith alphabet to current palindrome
//edges = list of palindromes that contains this palindrome as longest proper suffix that is a palindrome
//sufflink = longest palindrome that is proper suffix of current palindrome
//cnt = # occurence of this palindrome
} tree[N];
int num, suff;
//initialise the tree
void init(){
num = 2; suff = 2;
tree[0].len = -1; tree[0].sufflink = 0;
tree[1].len = 0; tree[1].sufflink = 0;
tree[0].edges.pb(1);
}
void add(int pos){
int cur = suff, curlen = 0;
int letter = s[pos]-'a';
while (true){
curlen = tree[cur].len;
if (pos-curlen-1 >= 0 && s[pos-curlen-1] == s[pos]){
break;
}
cur = tree[cur].sufflink;
}
num++;
suff = num;
tree[num].len = tree[cur].len + 2;
tree[num].cnt = 1;
tree[cur].nxt[letter] = num;
if (tree[num].len == 1){
tree[num].sufflink = 1;
tree[1].edges.pb(num);
return;
}
while (true){
cur = tree[cur].sufflink;
curlen = tree[cur].len;
if (pos-curlen-1 >= 0 && s[pos-curlen-1] == s[pos]){
int val = tree[cur].nxt[letter];
tree[num].sufflink = val;
tree[val].edges.pb(num);
break;
}
}
}
ll ans = 0;
void dfs(int u){
for (int v : tree[u].edges){
dfs(v);
tree[u].cnt += tree[v].cnt;
}
ans = max(ans, (ll) tree[u].cnt * (ll) tree[u].len);
}
void solve(){
cin >> s;
int n = s.size();
init();
FOR(i,0,n){
add(i);
}
dfs(0);
cout << ans;
return;
}
int main() {
ios::sync_with_stdio(false);
int TC = 1;
//cin >> TC;
FOR(i, 1, TC+1){
//cout << "Case #" << i << ": ";
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
palindrome.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
10 | #pragma GCC optimization ("Ofast")
|
palindrome.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
11 | #pragma GCC optimization ("unroll-loops")
|
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |