답안 #437653

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
437653 2021-06-26T18:07:35 Z soroush 회문 (APIO14_palindrome) C++17
100 / 100
427 ms 61220 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int , int> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 3e5+10;
const ll mod = 1e9+7;
const ll mod2 = 177013;
const ll base = 547;
const ll base2 = 31;

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)

int n;
string s;

struct hash{
	string s;
	ll H2[maxn], pw2[maxn];
	ll *h2;
	ll H[maxn], pw[maxn];
	ll *h;
	ll get(int l, int r){
		return (h[r] - (h[l - 1] * pw[r - l + 1])% mod + mod)%mod;
	}
	ll get2(int l, int r){
		return (h2[r] - (h2[l - 1] * pw2[r - l + 1])%mod2 + mod2)%mod2;
	}
	pair < ll, ll > Get(int l , int r){
		return pair < ll, ll >(get(l ,r) , get2(l ,r));
	}
	void init(string S){
		s = S;
		pw[0] = 1;
		for(int i = 1 ; i < maxn ; i ++)
			pw[i] = (pw[i - 1] * base)%mod;
		pw2[0] = 1;
		for(int i = 1 ; i < maxn ; i ++)
			pw2[i] = (pw2[i - 1] * base2)%mod2;
		h = &H[1];
		for(int i = 0 ; i < n ; i ++)	
			h[i] = ((h[i - 1] * base)%mod + s[i] - 'a' + 1)%mod;
		h2 = &H2[1];
		for(int i = 0 ; i < n ; i ++)	
			h2[i] = ((h2[i - 1] * base2)%mod2 + s[i] - 'a' + 1)%mod2;
	}
}h, hr;

vector < int > q[maxn];

int sa[maxn];
int rk[maxn];
int tp[maxn];
int cnt[maxn];
int lcp[maxn];

void SA(string &s){
	int A = 'z' , p = 0 , n = s.size();
	if(n == 1){
		sa[0] = rk[0] = 0;
		return;
	}
	for(int i = 0 ; i < n ; i ++)
		sa[i] = i , rk[i] = s[i];
	for(int j = 1 ; p < n - 1 ; j<<=1){
		p = 0;
		int k = (j>>1);
		for(int i = n - k ; i < n ; i ++)
			tp[p ++ ] = i;
		for(int i = 0 ; i < n ; i ++)
			if(sa[i] >= k)
				tp[p ++] = sa[i] - k;
		for(int i = 0 ; i <= A ; i ++)
			cnt[i] = 0;
		for(int i = 0 ; i < n ; i ++)
			cnt[rk[i]] ++;
		for(int i = 1 ; i <= A ; i ++)
			cnt[i] += cnt[i - 1];
		for(int i = n - 1 ; i >= 0 ; i --)
			sa[--cnt[rk[tp[i]]]] = tp[i];
		swap(rk , tp);
		rk[sa[0]] = p = 0;
		for(int i = 1 ; i < n ; i ++)
			p += (tp[sa[i - 1]]!=tp[sa[i]] || sa[i - 1] + k  >= n ||
			 tp[sa[i - 1]+k] != tp[sa[i] + k]) , rk[sa[i]] = p;
		A = p;
	}
}

vector < int > mrg[maxn];
int p[maxn], sz[maxn];

int gp(int v){
	return ((p[v] != -1) ? p[v] = gp(p[v]) : v);
}

void merge(int u, int v){
	if((u = gp(u)) == (v = gp(v)))
		return;
	sz[v] += sz[u];
	p[u] = v;
	
}

void LCP(string &s){
	for(int i = 0 , k = 0 ; i < (int)s.size() ; i ++){
		if(rk[i] == 0)continue;
		if(k) k --;
		while(s[i + k] == s[sa[rk[i] - 1] + k]) k ++;
		lcp[rk[i]] = k;
	}
}

void chk(int x){
	int l = 0, r = n;
	while(r - l > 1){
		int mid = (l + r) / 2;
		if(x + mid < n and x - mid > -1 and h.get(x - mid , x + mid) == hr.get(n - (x + mid) - 1 , n - (x - mid) - 1))
			l = mid;
		else 
			r = mid;
	}
	q[1 + 2 * l].pb(x - l);
	if(x > 0){
		int l = 0 , r = n ;
		if(s[x] ^ s[x - 1])return;
		while(r - l > 1){
			int mid = (l + r) / 2;
			if(x + mid < n and x - mid - 1 > -1 and h.get(x - mid - 1 , x + mid) == hr.get(n - (x + mid) - 1 , n - (x - mid - 1) - 1))
				l = mid;
			else 
				r = mid;
		}
		q[2 + 2 * l].pb(x - 1 - l);
	}
}


int32_t main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> s;
	n = s.size();
	h.init(s);
	reverse(s.begin(),s.end());
	hr.init(s);
	reverse(s.begin(),s.end());
	SA(s);
	LCP(s);
	ms(p, -1);
	for(int i = 0 ; i < n ; i ++)sz[i] = 1;
	for(int i = 1 ; i < n ; i ++)
		mrg[lcp[i]].pb(i - 1);
	for(int i = 0 ; i < n ; i ++)
		chk(i);
	ll ans = 0;
	for(int i = n ; i >= 0 ; i --){
		for(int x : mrg[i])
			merge(sa[x], sa[x + 1]);
		for(int x : q[i]){
			ans = max(ans, sz[gp(x)] * 1ll * i);
		}
	}
	cout << ans;
	return(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 27372 KB Output is correct
2 Correct 27 ms 27304 KB Output is correct
3 Correct 30 ms 27264 KB Output is correct
4 Correct 24 ms 25000 KB Output is correct
5 Correct 24 ms 27252 KB Output is correct
6 Correct 24 ms 27352 KB Output is correct
7 Correct 26 ms 27336 KB Output is correct
8 Correct 26 ms 27300 KB Output is correct
9 Correct 26 ms 27368 KB Output is correct
10 Correct 27 ms 27376 KB Output is correct
11 Correct 25 ms 27384 KB Output is correct
12 Correct 26 ms 27348 KB Output is correct
13 Correct 25 ms 27332 KB Output is correct
14 Correct 29 ms 27340 KB Output is correct
15 Correct 26 ms 27328 KB Output is correct
16 Correct 25 ms 27300 KB Output is correct
17 Correct 25 ms 27352 KB Output is correct
18 Correct 26 ms 27340 KB Output is correct
19 Correct 26 ms 27364 KB Output is correct
20 Correct 26 ms 27376 KB Output is correct
21 Correct 25 ms 27304 KB Output is correct
22 Correct 27 ms 27388 KB Output is correct
23 Correct 25 ms 27340 KB Output is correct
24 Correct 25 ms 27340 KB Output is correct
25 Correct 26 ms 27324 KB Output is correct
26 Correct 27 ms 27380 KB Output is correct
27 Correct 26 ms 27340 KB Output is correct
28 Correct 28 ms 27356 KB Output is correct
29 Correct 27 ms 27340 KB Output is correct
30 Correct 26 ms 27340 KB Output is correct
31 Correct 26 ms 27336 KB Output is correct
32 Correct 28 ms 27280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 27348 KB Output is correct
2 Correct 27 ms 27368 KB Output is correct
3 Correct 27 ms 27380 KB Output is correct
4 Correct 27 ms 27396 KB Output is correct
5 Correct 27 ms 27456 KB Output is correct
6 Correct 30 ms 27380 KB Output is correct
7 Correct 31 ms 27428 KB Output is correct
8 Correct 29 ms 27468 KB Output is correct
9 Correct 29 ms 27332 KB Output is correct
10 Correct 27 ms 27368 KB Output is correct
11 Correct 27 ms 27332 KB Output is correct
12 Correct 29 ms 27348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 28228 KB Output is correct
2 Correct 36 ms 28172 KB Output is correct
3 Correct 39 ms 28396 KB Output is correct
4 Correct 40 ms 28320 KB Output is correct
5 Correct 34 ms 28048 KB Output is correct
6 Correct 36 ms 27972 KB Output is correct
7 Correct 36 ms 28008 KB Output is correct
8 Correct 31 ms 27968 KB Output is correct
9 Correct 30 ms 27968 KB Output is correct
10 Correct 38 ms 28000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 104 ms 36948 KB Output is correct
2 Correct 102 ms 34836 KB Output is correct
3 Correct 127 ms 38468 KB Output is correct
4 Correct 128 ms 36312 KB Output is correct
5 Correct 120 ms 34088 KB Output is correct
6 Correct 139 ms 34368 KB Output is correct
7 Correct 125 ms 34648 KB Output is correct
8 Correct 81 ms 33596 KB Output is correct
9 Correct 152 ms 34484 KB Output is correct
10 Correct 173 ms 34648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 258 ms 56480 KB Output is correct
2 Correct 299 ms 48240 KB Output is correct
3 Correct 348 ms 61220 KB Output is correct
4 Correct 368 ms 50788 KB Output is correct
5 Correct 357 ms 48340 KB Output is correct
6 Correct 345 ms 48900 KB Output is correct
7 Correct 375 ms 48180 KB Output is correct
8 Correct 214 ms 46708 KB Output is correct
9 Correct 219 ms 46740 KB Output is correct
10 Correct 427 ms 48528 KB Output is correct
11 Correct 310 ms 54432 KB Output is correct
12 Correct 397 ms 46760 KB Output is correct