# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
854114 | GoldLight | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 187 ms | 760 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;
void fast(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
const int N=6000;
int a[N+5], b[N+1];
void f(string &s, int *c){
int j=0;
for(int i=1; i<s.size(); i++){
while(j && s[i]!=s[j]) j=c[j-1];
if(s[i]==s[j]) j++;
c[i]=j;
}
}
tuple<int,int,int> solve(string &s, string &t, bool rev){
int n=s.size(), m=t.size();
tuple<int,int,int> ans={0, 0, 0};
string t2=t;
reverse(t2.begin(), t2.end());
for(int i=0; i<n; i++){
string s1=s.substr(0, i), s2=s.substr(i, n-i);
reverse(s1.begin(), s1.end());
s1+='#'+t, s2+='#'+t2;
f(s1, a); f(s2, b);
for(int j=1; j<=m; j++){
ans=max(ans, {a[i+j]+b[n+m-i-j], i-a[i+j], rev? m-j-b[n+m-i-j] : j-a[i+j]});
}
}
return ans;
}
int main(){
fast();
string s, t; cin>>s>>t;
auto ans=solve(s, t, 0);
reverse(t.begin(), t.end());
ans=max(ans, solve(s, t, 1));
cout<<get<0>(ans)<<'\n'<<get<1>(ans)<<' '<<get<2>(ans);
}
/*
vector<int> f1(string &s, string &t){
int n=s.size(), m=t.size(), j=0;
vector<int> a(n), b(m);
for(int i=1; i<n; i++){
while(j>0 && s[i]!=s[j]) j=a[j-1];
if(s[i]==s[j]) j++;
a[i]=j;
}
j=0;
for(int i=0; i<m; i++){
while(j==n || (j>0 && s[j]!=t[i])) j=a[j-1];
if(s[j]==t[i]) j++;
b[i]=j;
}
return b;
}
vector<int> f2(string &s, string &t){
int n=s.size(), m=t.size(), j=0;
vector<int> a(n), b(m);
for(int i=1; i<n; i++){
while(j>0 && s[i]!=s[j]) j=a[j-1];
if(s[i]==s[j]) j++;
a[i]=j;
}
j=0;
for(int i=m-1; i>=0; i--){
while(j==n || (j>0 && s[j]!=t[i])) j=a[j-1];
if(s[j]==t[i]) j++;
b[i]=j;
}
return b;
}
int main(){
fast();
string s, t1, t2; cin>>s>>t1;
t2=t1; reverse(t2.begin(), t2.end());
int n=s.size(), m=t1.size();
int ki=1, ka=n, ans, j1, j2;
while(ki<=ka){
int k=(ki+ka)/2;
bool cek=false;
for(int l=0; l+k-1<n; l++){
if(cek) break;
if(k>m){
cek=true;
break;
}
string s1=s.substr(l, k);
string s2=s1; reverse(s2.begin(), s2.end());
auto val1=f1(s1, t1), val2=f2(s2, t1), val3=f1(s1, t2), val4=f2(s2, t2);
for(int i=0; i+k-1<m; i++){
if(val1[i+k-1]+val2[i]>=k){
ans=k, j1=l, j2=i;
cek=true;
break;
}
}
for(int i=0; i+k-1<m; i++){
if(val3[i+k-1]+val4[i]>=k){
ans=k, j1=l, j2=m-(i+k-1)-1;
cek=true;
break;
}
}
// if(ans==k){
// cout<<s1<<'\n';
// for(int i=0; i<m; i++) cout<<val3[i]<<' ';
// cout<<'\n';
// for(int i=0; i<m; i++) cout<<val4[i]<<' ';
// cout<<'\n';
// }
}
if(cek) ki=k+1;
else ka=k-1;
}
cout<<ans<<'\n';
cout<<j1<<' '<<j2;
}
*/
//KMP
/*
vector<int> f(string &s){
int n=s.size(), j=0;
vector<int> v(n);
for(int i=1; i<n; i++){
while(j>0 && s[i]!=s[j]) j=v[j-1];
if(s[i]==s[j]) j++;
v[i]=j;
}
return v;
}
int main(){
fast();
string s, t; cin>>s>>t;
string temp=t+'0'+s;
auto lps=f(temp);
int ans=0;
for(auto i:lps) ans+=(i==t.size());
cout<<ans;
}*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |