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>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3NRqilq ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
#define all(a) a.begin(),a.end()
vi eval_pf(const string&s){
int n=sz(s);
vi pf(n);
rng(i,1,n){
int j=pf[i-1];
while(j and s[i]!=s[j]){
j=pf[j-1];
}
j+=(s[i]==s[j]);
pf[i]=j;
}
return pf;
}
// cp algorithms Z-function and its calculation
vector<int> z_function(string s) {
int n = (int) s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = min (r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
++z[i];
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
signed main(){
_3NRqilq;
string s;
cin>>s;
// s=string(3000,'a');
string t;
// t=string(3000,'a');
cin>>t;
string rt=t;
reverse(all(rt));
int n=sz(s);
int m=sz(t);
int len=0;
pii p={0,0};
vi a(m),b(m);
vec(pii) dp(m+1);
rep(_,2){
rep(i,n)
{
if(i<n-1){
string cur=s.substr(i+1,n-i-1);
cur+=".";
cur+=rt;
auto pfa=eval_pf(cur);
rng(j,n-i,sz(cur)){
a[m-(j-(n-i))-1]=pfa[j];
}
}
if(i){
string cur="";
cur+=s.substr(0,i);
reverse(all(cur));
cur+=".";
cur+=t;
auto pfb=z_function(cur);
rng(j,i+1,sz(cur)){
b[j-(i+1)]=pfb[j];
// cout<<pfb[j]<<" ";
}
}
rep(j,m+1){
dp[j]=pii(j==m?m:(j+a[j]),j);
if(j){
dp[j]=max(dp[j],dp[j-1]);
}
}
rep(j,m){
if(s[i]==t[j]){
if(j+1<m){
int frm=j+1;
int to=frm+b[frm]-1;
pii q=dp[to+1];
int now=q.fi-frm+1;
if(now>len){
len=now;
int ni=i-(q.se-frm);
p={ni,j};
if(_==1){
p={n-ni-now,j};
}
}
}
{
int now=1;
if(now>len){
len=now;
int ni=i;
p={ni,j};
if(_==1){
p={n-ni-now,j};
}
}
}
}
}
}
reverse(all(s));
}
cout<<len<<"\n";
cout<<p.fi<<" "<<p.se<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |