# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
530037 | vivo2006 | Necklace (Subtask 4) (BOI19_necklace4) | C++14 | 311 ms | 432 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.
//BOI 2019 - Necklace
#include<bits/stdc++.h>
using namespace std;
int p1[6002], p2[6002];
string s1, s2;
pair<pair<int, int>, int> ans;
void KMP(string s, int* p)
{
int i, len;
for(i = 1; i < s.size(); i ++)
{
len = p[i - 1];
while(len && s[i] != s[len]) len = p[len - 1];
if(s[i] == s[len]) p[i] = len + 1;
else p[i] = 0;
}
}
int main()
{
int i, j, lft, rgt;
cin>>s1>>s2;
string leftS2 = s2, rightS2 = s2;
reverse(leftS2.begin(), leftS2.end());
for(i = 0; i < s1.size(); i ++)
{
string leftS1 = s1.substr(0, i), rightS1 = s1.substr(i);
reverse(leftS1.begin(), leftS1.end());
KMP(leftS1 + "#" + rightS2, p1);
KMP(rightS1 + "#" + leftS2, p2);
for(j = 1; j <= s2.size(); j ++)
{
ans = max(ans, {{p1[i + j] + p2[s1.size() + s2.size() - i - j], i - p1[i + j]}, j - p1[i + j]});
//cout<<i<<" "<<j<<" : "<<p1[i + j]<<" "<<p2[s1.size() + s2.size() - i - j]<<endl;
}
}
reverse(rightS2.begin(), rightS2.end());
reverse(leftS2.begin(), leftS2.end());
for(i = 0; i < s1.size(); i ++)
{
string leftS1 = s1.substr(0, i), rightS1 = s1.substr(i);
reverse(leftS1.begin(), leftS1.end());
KMP(leftS1 + "#" + rightS2, p1);
KMP(rightS1 + "#" + leftS2, p1);
for(j = 1; j <= s2.size(); j ++)
{
ans = max(ans, {{p1[i + j] + p2[s1.size() + s2.size() - i - j], i - p1[i + j]}, s2.size() - j - p2[s1.size() + s2.size() - i - j]});
}
}
cout<<ans.first.first<<"\n"<<ans.first.second<<" "<<ans.second<<endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |