Submission #356832

# Submission time Handle Problem Language Result Execution time Memory
356832 2021-01-23T19:07:21 Z Kerim Palindromes (APIO14_palindrome) C++17
100 / 100
683 ms 51536 KB
#include "bits/stdc++.h"
#define MAXN 300009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;
 
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int LOG=20;
int S[MAXN][LOG],ord[MAXN],lcp[MAXN],rad[MAXN<<1],ext[MAXN],vis[MAXN],n;
pair<PII,int> C[MAXN],H[MAXN];
char s[MAXN],tmp[MAXN<<1];
void fastsort(){
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
		vis[C[i].ff.ss]++;
	for(int i=1;i<MAXN;i++)
		vis[i]+=vis[i-1];
	for(int i=n;i>=1;i--)
		H[vis[C[i].ff.ss]--]=C[i];
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
		vis[H[i].ff.ff]++;
	for(int i=1;i<MAXN;i++)
		vis[i]+=vis[i-1];	
	for(int i=n;i>=1;i--)
		C[vis[H[i].ff.ff]--]=H[i];	
}
int ata[MAXN],sz[MAXN];
int tap(int x){
	if(ata[x]==x)return x;
	return ata[x]=tap(ata[x]);
}	
void merge(int x,int y){
	if((x=tap(x))==(y=tap(y)))
		return;
	if(ext[x]<ext[y])swap(x,y);
	ata[y]=x;sz[x]+=sz[y];ext[x]+=ext[y];
}
int main(){
    //freopen("file.in", "r", stdin);
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
    	S[i][0]=s[i]-'a'+1;
    for(int j=1;j<LOG;j++){
    	for(int i=1;i<=n;i++)
			C[i]=mp(mp(S[i][j-1],(i+(1<<(j-1))<=n?S[i+(1<<(j-1))][j-1]:0)),i);
		//sort(c+1,c+n+1,cmp);
		fastsort();
		for(int i=1;i<=n;i++)
			S[C[i].ss][j]=S[C[i-1].ss][j]+(C[i].ff!=C[i-1].ff);	
    }
    for(int i=1;i<=n;i++)
    	ord[S[i][LOG-1]]=i;
    vector<pair<int,PII> >v;
    vector<PII>g;
    for(int i=1;i<n;i++){
    	int a=ord[i],b=ord[i+1];
		for(int j=LOG-1;j>=0;j--)
			if(a<=n and b<=n and S[a][j]==S[b][j])
				a+=(1<<j),b+=(1<<j),lcp[i]+=(1<<j);
		v.pb(mp(lcp[i],mp(ord[i],ord[i+1])));
    }
    sort(all(v),greater<pair<int,PII>>());
	for(int i=0;i<2*n+1;i++)tmp[i]='#';
	for(int i=0;i<n;i++)tmp[i*2+1]=s[i+1];
	for(int i=0,j=0,k;i<n*2+1;i+=k){
		while(i-j>=0&&i+j<n*2+1&&tmp[i-j]==tmp[i+j])j++;
		rad[i]=j;k=1;
		while(rad[i-k]<rad[i]-k)rad[i+k]=rad[i-k],k++;
		j=max(0,j-k);
	}
    //odd
	for(int i=1;i<=n;i++)g.pb(mp(rad[i*2-1]/2,i));
	sort(all(g),greater<PII>());
	int p=0;ll ans=0;
	for(int i=1;i<=n;i++)ata[i]=i,sz[i]=0,ext[i]=1;
	for(int i=0;i<n;i++){
		while(p<int(v.size()) and v[p].ff>=g[i].ff)
			merge(v[p].ss.ff,v[p].ss.ss),p++;
		sz[tap(g[i].ss)]++;
		umax(ans,sz[tap(g[i].ss)]*1LL*(g[i].ff*2-1));
	}
	//even
	g.clear();
	for(int i=1;i<n;i++)g.pb(mp(rad[i*2]/2,i+1));
	sort(all(g),greater<PII>());p=0;
	for(int i=1;i<=n;i++)ata[i]=i,sz[i]=0,ext[i]=1;
	for(int i=0;i<n-1;i++){
		while(p<int(v.size()) and v[p].ff>=g[i].ff)
			merge(v[p].ss.ff,v[p].ss.ss),p++;
		sz[tap(g[i].ss)]++;
		umax(ans,sz[tap(g[i].ss)]*1LL*(g[i].ff*2));
	}
	printf("%lld\n",ans);
	return 0;
}

Compilation message

palindrome.cpp: In function 'int main()':
palindrome.cpp:53:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   53 |     scanf("%s",s+1);
      |     ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 37 ms 1516 KB Output is correct
2 Correct 37 ms 1516 KB Output is correct
3 Correct 37 ms 1516 KB Output is correct
4 Correct 37 ms 1516 KB Output is correct
5 Correct 37 ms 1676 KB Output is correct
6 Correct 37 ms 1516 KB Output is correct
7 Correct 38 ms 1516 KB Output is correct
8 Correct 37 ms 1516 KB Output is correct
9 Correct 37 ms 1516 KB Output is correct
10 Correct 38 ms 1516 KB Output is correct
11 Correct 37 ms 1644 KB Output is correct
12 Correct 37 ms 1516 KB Output is correct
13 Correct 37 ms 1592 KB Output is correct
14 Correct 37 ms 1516 KB Output is correct
15 Correct 37 ms 1516 KB Output is correct
16 Correct 37 ms 1516 KB Output is correct
17 Correct 37 ms 1536 KB Output is correct
18 Correct 37 ms 1516 KB Output is correct
19 Correct 37 ms 1644 KB Output is correct
20 Correct 38 ms 1536 KB Output is correct
21 Correct 37 ms 1644 KB Output is correct
22 Correct 37 ms 1644 KB Output is correct
23 Correct 37 ms 1644 KB Output is correct
24 Correct 38 ms 1644 KB Output is correct
25 Correct 39 ms 1644 KB Output is correct
26 Correct 37 ms 1536 KB Output is correct
27 Correct 37 ms 1644 KB Output is correct
28 Correct 37 ms 1644 KB Output is correct
29 Correct 37 ms 1644 KB Output is correct
30 Correct 38 ms 1644 KB Output is correct
31 Correct 37 ms 1644 KB Output is correct
32 Correct 37 ms 1644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 1772 KB Output is correct
2 Correct 38 ms 1772 KB Output is correct
3 Correct 38 ms 1772 KB Output is correct
4 Correct 38 ms 1772 KB Output is correct
5 Correct 40 ms 1772 KB Output is correct
6 Correct 38 ms 1772 KB Output is correct
7 Correct 38 ms 1772 KB Output is correct
8 Correct 38 ms 1772 KB Output is correct
9 Correct 38 ms 1772 KB Output is correct
10 Correct 38 ms 1772 KB Output is correct
11 Correct 39 ms 1772 KB Output is correct
12 Correct 39 ms 1772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 3244 KB Output is correct
2 Correct 47 ms 3192 KB Output is correct
3 Correct 47 ms 3244 KB Output is correct
4 Correct 47 ms 3244 KB Output is correct
5 Correct 51 ms 3244 KB Output is correct
6 Correct 48 ms 3244 KB Output is correct
7 Correct 46 ms 3244 KB Output is correct
8 Correct 48 ms 3244 KB Output is correct
9 Correct 51 ms 3244 KB Output is correct
10 Correct 48 ms 3244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 174 ms 17760 KB Output is correct
2 Correct 152 ms 17780 KB Output is correct
3 Correct 155 ms 17760 KB Output is correct
4 Correct 162 ms 17760 KB Output is correct
5 Correct 195 ms 17760 KB Output is correct
6 Correct 181 ms 17760 KB Output is correct
7 Correct 165 ms 17760 KB Output is correct
8 Correct 218 ms 17760 KB Output is correct
9 Correct 198 ms 17760 KB Output is correct
10 Correct 202 ms 17760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 441 ms 51152 KB Output is correct
2 Correct 419 ms 51060 KB Output is correct
3 Correct 391 ms 51152 KB Output is correct
4 Correct 402 ms 51040 KB Output is correct
5 Correct 536 ms 51060 KB Output is correct
6 Correct 456 ms 51408 KB Output is correct
7 Correct 470 ms 51408 KB Output is correct
8 Correct 683 ms 51408 KB Output is correct
9 Correct 675 ms 51408 KB Output is correct
10 Correct 577 ms 51480 KB Output is correct
11 Correct 449 ms 51536 KB Output is correct
12 Correct 631 ms 51408 KB Output is correct