답안 #367261

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
367261 2021-02-16T17:27:39 Z Bill_00 회문 (APIO14_palindrome) C++14
100 / 100
192 ms 110220 KB
#include <bits/stdc++.h>
#define NN 300000
#define pb push_back
typedef int ll;
using namespace std;
long long ans;
int w=0;
int b[20][NN];
struct pal{
	ll cnt;
	pal *a[26];
	ll h;
	ll id;
}*root=new pal(),*root1=new pal();
pal *u,*ptr[NN],*p[NN];
void ins(pal *rt,ll k){
	if(rt->a[k]==NULL){
		rt->a[k]=new pal();	
		(rt->a[k])->h=(rt->h)+1;
		rt->a[k]->id=++w;
		b[0][w]=rt->id;
		for(int i=1;i<20;i++){
			b[i][w]=b[i-1][b[i-1][w]];
		}
		p[w]=rt->a[k];
	} 
	u=rt->a[k];
}
int solve(pal *rt,ll length,bool b){
	for(ll i=0;i<26;i++){
		if(rt->a[i]!=NULL){
			rt->cnt+=(solve(rt->a[i],length+1,b));
		}
	}
	ans=max(ans,(long long)((long long)(rt->cnt)*(long long)(b==1?(length*2-1):(length*2))));
	int o=rt->cnt;
	delete(rt);
	rt=NULL;
	return o;
}
int main(){
	p[0]=root;
	string s;
	cin >> s;
	ll n=s.size();
	vector<ll>d1(n);
	for (ll i = 0, l = 0, r = -1; i < n; i++) {
    	ll k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1);
    	if(r>=i){
    		if(d1[l+r-i]<=(r-i+1)){
    			u=ptr[l+r-i];
    		}
    		else{
    			int y=ptr[l+r-i]->id;
    			for(ll j=19;j>=0;j--){
    				if(((p[b[j][y]])->h)>=(r-i+1)){
    					y=b[j][y];	
    				} 
    			}
    			u=p[y];
    		}
    	}
    	else ins(root,(s[i]-'a'));
    	while (0 <= i - k && i + k < n && s[i - k] == s[i + k]){
    		ins(u,(s[i-k]-'a'));
       		k++;
    	}
    	d1[i] = k--;
    	ptr[i]=u;
    	(u->cnt)++;
    	if (i + k > r) {
       		l = i - k;
        	r = i + k;
    	}
	}
	solve(root,0,1);
	p[0]=root1;
	vector<ll>d2(n);
	for(ll i=0,l=0,r=-1;i<n;i++){
		ll k=((i>r)?0:min(d2[l+r-i+1],r-i+1));
		if(r>=i){
			if(d2[l+r-i+1]<=r-i+1){
				u=ptr[l+r-i+1];
			}	
			else{
				int y=ptr[l+r-i+1]->id;
				for(ll j=19;j>=0;j--){
					if(((p[b[j][y]])->h)>=(r-i+1)){
    					y=b[j][y];	
    				} 
				}
				u=p[y];
			}
		} 
		else u=root1;
		while(0<=i-k-1 && i+k<n && s[i-k-1]==s[i+k]){
			ins(u,(s[i+k]-'a'));
			k++;
		}
		d2[i]=k--;
		ptr[i]=u;
		(u->cnt)++;
		if(i+k>r){
			l=i-k-1;
			r=i+k;
		}
	}
	solve(root1,0,0);
	cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 1 ms 492 KB Output is correct
26 Correct 1 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 1 ms 492 KB Output is correct
30 Correct 1 ms 492 KB Output is correct
31 Correct 1 ms 492 KB Output is correct
32 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 748 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Correct 1 ms 748 KB Output is correct
4 Correct 1 ms 748 KB Output is correct
5 Correct 1 ms 748 KB Output is correct
6 Correct 1 ms 748 KB Output is correct
7 Correct 1 ms 748 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 4076 KB Output is correct
2 Correct 5 ms 3948 KB Output is correct
3 Correct 5 ms 2924 KB Output is correct
4 Correct 6 ms 2960 KB Output is correct
5 Correct 5 ms 3820 KB Output is correct
6 Correct 6 ms 3692 KB Output is correct
7 Correct 5 ms 3820 KB Output is correct
8 Correct 1 ms 748 KB Output is correct
9 Correct 1 ms 748 KB Output is correct
10 Correct 4 ms 2924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 36972 KB Output is correct
2 Correct 67 ms 35692 KB Output is correct
3 Correct 59 ms 25836 KB Output is correct
4 Correct 56 ms 24684 KB Output is correct
5 Correct 46 ms 36076 KB Output is correct
6 Correct 45 ms 26220 KB Output is correct
7 Correct 41 ms 22368 KB Output is correct
8 Correct 6 ms 2540 KB Output is correct
9 Correct 20 ms 7748 KB Output is correct
10 Correct 46 ms 30828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 110220 KB Output is correct
2 Correct 172 ms 103308 KB Output is correct
3 Correct 192 ms 76172 KB Output is correct
4 Correct 170 ms 70148 KB Output is correct
5 Correct 129 ms 108940 KB Output is correct
6 Correct 159 ms 78988 KB Output is correct
7 Correct 121 ms 66412 KB Output is correct
8 Correct 16 ms 6156 KB Output is correct
9 Correct 16 ms 6156 KB Output is correct
10 Correct 125 ms 92684 KB Output is correct
11 Correct 171 ms 110092 KB Output is correct
12 Correct 29 ms 12356 KB Output is correct