Submission #220276

# Submission time Handle Problem Language Result Execution time Memory
220276 2020-04-07T13:42:50 Z LittleFlowers__ Palindromes (APIO14_palindrome) C++14
73 / 100
1000 ms 56868 KB
#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define gg exit(0);

const int N=600010;

int n,gap;
long long ret;
int pos[N],sa[N],tmp[N],lcp[N],ln[N],g[N];
int f[22][N];
string s,ss;

int cmp(int i,int j){
    if(pos[i]!=pos[j]) return pos[i]<pos[j];
    i+=gap, j+=gap;
    return i<=n && j<=n ? pos[i]<pos[j] : i>j;
}

int que(int i,int j){
    int t=ln[j-i+1];
    return min(f[t][i],f[t][j-(1<<t)+1]);
}


int calc(int l,int r){
    int lf=pos[l],rt=pos[l];
    int le=pos[l],ri=n,mi;
    while(le<=ri){
        mi=(le+ri)/2;
        if(que(pos[l],mi-1)>=r-l+1) rt=mi, le=mi+1;
        else ri=mi-1;
    }
    le=1,ri=pos[l];
    while(le<=ri){
        mi=(le+ri)/2;
        if(que(mi,pos[l]-1)>=r-l+1) lf=mi, ri=mi-1;
        else le=mi+1;
    }
    return (rt-lf+1);
}

int p[N],r[N];
main(){
    #define task "palindrome"
    //freopen(task".inp","r",stdin);
    //freopen(task".out","w",stdout);
    fasty;

    cin>>s; n=s.size();
    ss=s; s="$";
    forinc(i,1,n) s+="#", s+=ss[i-1]; s+="#"; n=s.size()-1;
    forinc(i,1,n) sa[i]=i, pos[i]=s[i], ln[i]=log2(i);
    for(gap=1;;gap+=gap){
        sort(sa+1,sa+n+1,cmp);
        forinc(i,1,n-1) tmp[i+1]=tmp[i]+cmp(sa[i],sa[i+1]);
        forinc(i,1,n) pos[sa[i]]=tmp[i]+1;
        if(tmp[n]==n-1) break;
    }
    for(int i=1,k=0;i<=n;++i){
        for(int j=sa[pos[i]+1];s[i+k]==s[j+k];) k++;
        lcp[pos[i]]=k;
        if(k) k--;
    }

    forinc(i,1,n) f[0][i]=lcp[i];
    forinc(j,1,20) forinc(i,1,n){
        if(i+(1<<j)>n+1) continue;
        f[j][i]=min(f[j-1][i],f[j-1][i+(1<<j-1)]);
    }

    int mx=0,id=0,i,j,k,x;
    forinc(i,1,n){
        if (mx>=i) g[i]=min(mx-i,g[2*id-i]);
        else g[i]=0;
        while(i-g[i]-1>=1&&i+g[i]+1<=n&&s[i+g[i]+1]==s[i-g[i]-1]) g[i]++;
        if(i+g[i]>mx){
            for (x=mx-i+2;x<=g[i];x+=2){
              ret=max(ret,(long long)calc(i-x,i+x)*x);
            }
            mx=i+g[i];
            id=i;
        }
    }
    cout<<ret;
}

Compilation message

palindrome.cpp:60:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
palindrome.cpp: In function 'int main()':
palindrome.cpp:7:23: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
 #define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
                       ^
palindrome.cpp:68:5: note: in expansion of macro 'forinc'
     forinc(i,1,n) s+="#", s+=ss[i-1]; s+="#"; n=s.size()-1;
     ^~~~~~
palindrome.cpp:68:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
     forinc(i,1,n) s+="#", s+=ss[i-1]; s+="#"; n=s.size()-1;
                                       ^
palindrome.cpp:85:45: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
         f[j][i]=min(f[j-1][i],f[j-1][i+(1<<j-1)]);
                                            ~^~
palindrome.cpp:88:19: warning: unused variable 'i' [-Wunused-variable]
     int mx=0,id=0,i,j,k,x;
                   ^
palindrome.cpp:88:21: warning: unused variable 'j' [-Wunused-variable]
     int mx=0,id=0,i,j,k,x;
                     ^
palindrome.cpp:88:23: warning: unused variable 'k' [-Wunused-variable]
     int mx=0,id=0,i,j,k,x;
                       ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 512 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 5 ms 512 KB Output is correct
24 Correct 5 ms 512 KB Output is correct
25 Correct 5 ms 512 KB Output is correct
26 Correct 5 ms 512 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 512 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 512 KB Output is correct
32 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 640 KB Output is correct
2 Correct 6 ms 640 KB Output is correct
3 Correct 6 ms 640 KB Output is correct
4 Correct 6 ms 640 KB Output is correct
5 Correct 6 ms 640 KB Output is correct
6 Correct 6 ms 640 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 5 ms 640 KB Output is correct
11 Correct 5 ms 640 KB Output is correct
12 Correct 6 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 2048 KB Output is correct
2 Correct 23 ms 2048 KB Output is correct
3 Correct 26 ms 2048 KB Output is correct
4 Correct 26 ms 2044 KB Output is correct
5 Correct 24 ms 2176 KB Output is correct
6 Correct 24 ms 2048 KB Output is correct
7 Correct 22 ms 2176 KB Output is correct
8 Correct 15 ms 2048 KB Output is correct
9 Correct 15 ms 2176 KB Output is correct
10 Correct 25 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 18656 KB Output is correct
2 Correct 288 ms 18784 KB Output is correct
3 Correct 306 ms 18784 KB Output is correct
4 Correct 284 ms 18784 KB Output is correct
5 Correct 395 ms 18788 KB Output is correct
6 Correct 321 ms 18784 KB Output is correct
7 Correct 280 ms 18912 KB Output is correct
8 Correct 156 ms 18784 KB Output is correct
9 Correct 289 ms 18784 KB Output is correct
10 Correct 384 ms 18912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 56868 KB Time limit exceeded
2 Halted 0 ms 0 KB -