# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1076796 | vjudge1 | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 301 ms | 600 KiB |
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 ll long long
#define fi first
#define se second
string s;
string t;
string s1,s2;
int L[3003],R[3003],dp1[3003],dp2[3003],m,n,k,cs1,cs2;
int maxx;
int riel(int x)
{
return t.size()-1-x;
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>s;
cin>>t;
for (int i=0;i<s.size();++i)
{
s1=" ";
s2=" ";
for (int j=i;j>=0;--j) s1+=s[j];
for (int j=i+1;j<s.size();++j) s2+=s[j];
L[1]=0;
k=0;
m=s1.size()-1;
n=s2.size()-1;
for (int j=2;j<=m;++j)
{
while (k>0 && s1[j]!=s1[k+1]) k=L[k];
if (s1[j]==s1[k+1]) L[j]=++k;
else L[j]=0;
}
k=0;
R[1]=0;
for (int j=2;j<=n;++j)
{
while (k>0 && s2[j]!=s2[k+1]) k=R[k];
if (s2[j]==s2[k+1]) R[j]=++k;
else R[j]=0;
}
k=0;
for (int j=0;j<t.size();++j)
{
while (k>0 && t[j]!=s2[k+1]) k=R[k];
if (t[j]==s2[k+1]) dp1[j]=++k;
else dp1[j]=0;
}
k=0;
for (int j=t.size()-1;j>=0;--j)
{
while (k>0 && t[j]!=s1[k+1]) k=L[k];
if (t[j]==s1[k+1]) dp2[j]=++k;
else dp2[j]=0;
}
for (int j=0;j<t.size();++j)
{
if (maxx<dp1[j]+dp2[j+1])
{
maxx=dp1[j]+dp2[j+1];
cs1=i-dp2[j+1]+1;
cs2=j-dp1[j]+1;
}
}
}
reverse(t.begin(),t.end());
for (int i=0;i<s.size();++i)
{
s1=" ";
s2=" ";
for (int j=i;j>=0;--j) s1+=s[j];
for (int j=i+1;j<s.size();++j) s2+=s[j];
L[1]=0;
k=0;
m=s1.size()-1;
n=s2.size()-1;
for (int j=2;j<=m;++j)
{
while (k>0 && s1[j]!=s1[k+1]) k=L[k];
if (s1[j]==s1[k+1]) L[j]=++k;
else L[j]=0;
}
k=0;
R[1]=0;
for (int j=2;j<=n;++j)
{
while (k>0 && s2[j]!=s2[k+1]) k=R[k];
if (s2[j]==s2[k+1]) R[j]=++k;
else R[j]=0;
}
k=0;
for (int j=0;j<t.size();++j)
{
while (k>0 && t[j]!=s2[k+1]) k=R[k];
if (t[j]==s2[k+1]) dp1[j]=++k;
else dp1[j]=0;
}
k=0;
for (int j=t.size()-1;j>=0;--j)
{
while (k>0 && t[j]!=s1[k+1]) k=L[k];
if (t[j]==s1[k+1]) dp2[j]=++k;
else dp2[j]=0;
}
for (int j=0;j<t.size();++j)
{
if (maxx<dp1[j]+dp2[j+1])
{
maxx=dp1[j]+dp2[j+1];
cs1=i-dp2[j+1]+1;
cs2=t.size()-1-(j+dp2[j+1]);
//cout<<dp1[j]<<" "<<dp2[j+1]<<" "<<j<<'\n';
}
}
}
cout<<maxx<<'\n';
cout<<cs1<<" "<<cs2;
//cout<<dp2[6]<<" "<<dp1[5];
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |