#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void pf_function(const string &s, vector<int> &p)
{
p.resize(s.size());
p[0] = 0;
for (int i = 1; i < s.size(); i++)
{
int j = p[i-1];
while (j && s[i] != s[j]) j = p[j-1];
if (s[i] == s[j]) j++;
p[i] = j;
}
/*cout << "\n" << s << "\n";
for (int i = 0; i < p.size(); i++) cout << p[i] << " ";
cout << endl;*/
}
vector<int> best(const string &s, const string &t, bool rv)
{
int n = s.size(), m = t.size();
vector<int> ans = {0, 0, 0};
for (int i = 0; i < n; i++) // skusime kde rozrezat s
{
//cout << i << endl;
string s1 = s.substr(0, i), s2 = s.substr(i, n-i);
string t1 = t;
reverse(t1.begin(), t1.end()), reverse(s1.begin(), s1.end());
vector<int> v1, v2;
pf_function(s1 + "#" + t1, v1);
pf_function(s2 + "#" + t, v2);
for (int j = 0; j < m; j++)
{
int a = v1[v1.size() - j - 1], b = v2[s2.size() + j];
int si = i - a, ti = j - b;
if (rv) ti = m - (ti+a+b-1) - 1;
ans = max(ans, {a+b, si, ti});
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
vector<int> ans = best(s, t, false);
reverse(t.begin(), t.end());
ans = max(ans, best(s, t, true));
cout << ans[0] << "\n" << ans[1] << " " << ans[2] << "\n";
return 0;
}
Compilation message
necklace.cpp: In function 'void pf_function(const string&, std::vector<int>&)':
necklace.cpp:9:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | for (int i = 1; i < s.size(); i++)
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
671 ms |
332 KB |
Output is correct |
2 |
Correct |
624 ms |
452 KB |
Output is correct |
3 |
Correct |
700 ms |
584 KB |
Output is correct |
4 |
Correct |
577 ms |
432 KB |
Output is correct |
5 |
Correct |
506 ms |
508 KB |
Output is correct |
6 |
Correct |
552 ms |
512 KB |
Output is correct |
7 |
Correct |
589 ms |
452 KB |
Output is correct |
8 |
Correct |
607 ms |
436 KB |
Output is correct |
9 |
Correct |
620 ms |
436 KB |
Output is correct |