This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=1e6+5;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
struct node{
ll length,start,end,cnt;
ll insert_edge[26];
ll suffix_edge;
};
node tree[N];
string s;
ll ptr,cur;
ll vis[N];
void insert(ll id){
ll tmp=cur;
while(1){
if(id-tree[tmp].length>=1&&s[id]==s[id-tree[tmp].length-1])break;
tmp=tree[tmp].suffix_edge;
}
if(tree[tmp].insert_edge[s[id]-'a']!=0){
cur=tree[tmp].insert_edge[s[id]-'a'];
++tree[tmp].cnt;
return ;
}
//build new node
++ptr;
tree[tmp].insert_edge[s[id]-'a']=ptr;
tree[ptr].length=tree[tmp].length+2;
tree[ptr].end=id;
tree[ptr].start=id-tree[ptr].length+1;
tree[ptr].cnt=1;
// suffix_edge
tmp=tree[tmp].suffix_edge;
cur=ptr;
if(tree[cur].length==1){
tree[cur].suffix_edge=2;
return;
}
while(1){
if(id-tree[tmp].length>=1&&s[id]==s[id-tree[tmp].length-1])break;
tmp=tree[tmp].suffix_edge;
}
tree[cur].suffix_edge=tree[tmp].insert_edge[s[id]-'a'];
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
tree[1].length=-1;
tree[1].suffix_edge=1;
tree[2].length=0;
tree[2].suffix_edge=1;
ptr=2;
cur=1;
cin>>s;
REP(i,SZ(s))insert(i);
stack<ll> stk;
queue<ll> que;
stk.push(1);stk.push(2);que.push(1);que.push(2);
vis[1]=vis[2]=1;
while(SZ(que)){
ll x=que.front();que.pop();
REP(i,26){
ll y=tree[x].insert_edge[i];
if(y!=0&&!vis[y]){
stk.push(y);que.push(y);vis[y]=1;
}
}
}
while(SZ(stk)){
ll x=stk.top();stk.pop();
tree[tree[x].suffix_edge].cnt+=tree[x].cnt;
}
ll ans=0;
for(int i=3;i<=ptr;i++)ans=max(ans,tree[i].length*tree[i].cnt);
cout<<ans<<"\n";
return 0;
}
# | 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... |