# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
504013 | hieupy2k5 | Necklace (Subtask 4) (BOI19_necklace4) | C++14 | 247 ms | 516 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;
string s , t;
int p1[12345] , p2[12345];
struct Data
{
int l , pos_s , pos_t;
};
Data Max(Data a , Data b)
{
return (a.l > b.l) ? a : b;
}
Data get(string s , string t , bool ok)
{
int n = s.size() , m = t.size();
Data res = {0 , 0 , 0};
for(int i = 0 ; i < n ; i++)
{
string s1 = s.substr(0 , i) , s2 = s.substr(i , n - 1) , t1 = t , t2 = t;
reverse(s1.begin() , s1.end());
reverse(t2.begin() , t2.end());
s1 += '.' + t1;
s2 += '.' + t2;
for(int j = 1 ; j < s1.size() ; j++)
{
int k = p1[j - 1];
while(k && s1[j] != s1[k]) k = p1[k - 1];
if(s1[j] == s1[k]) k++;
p1[j] = k;
}
for(int j = 1 ; j < s2.size() ; j++)
{
int k = p2[j - 1];
while(k && s2[j] != s2[k]) k = p2[ - 1];
if(s2[j] == s2[k]) k++;
p2[j] = k;
}
for (int j = 1; j <= m; j++)
res = Max(res , {
p1[i + j] + p2[n + m - i - j] ,
i - p1[i + j] ,
ok ? m - j - p2[n + m - i - j] : j - p1[i + j]
});
}
return res;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
#define name "necklace"
if(fopen(name".inp" , "r"))
{
freopen(name".inp" , "r" , stdin);
freopen(name".out" , "w" , stdout);
}
cin>>s>>t;
Data res = get(s , t , 0);
reverse(t.begin() , t.end());
res = Max(res , get(s , t , 1));
cout<<res.l<<'\n';
cout<<res.pos_s<<' '<<res.pos_t;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |