# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
471061 | KhaledFarhat | Necklace (Subtask 4) (BOI19_necklace4) | C++14 | 345 ms | 456 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 F first
#define S second
typedef pair<int, int> pi;
class KmpSolver {
public:
vector<int> getPrefixFunction(const string& s) { // tested
vector<int> pi(s.size());
for (int i = 1; i < s.size(); ++i) {
for (int j = pi[i - 1]; j > 0; j = pi[j - 1]) {
if (s[i] == s[j]) {
pi[i] = j + 1;
break;
}
}
if (pi[i] == 0 && s[i] == s[0]) {
pi[i] = 1;
}
}
return pi;
}
};
tuple<int, int, int> Solve(string s, string t, bool wasSReversed) {
int n = s.size();
int m = t.size();
KmpSolver solver;
tuple<int, int, int> answer;
string reversedT = t;
reverse(reversedT.begin(), reversedT.end());
for (int firstPartInS = 0; firstPartInS <= n; ++firstPartInS) {
string sPrefix = s.substr(0, firstPartInS);
reverse(sPrefix.begin(), sPrefix.end());
string firstText = sPrefix + "#" + reversedT;
vector<int> firstPi = solver.getPrefixFunction(firstText);
string sSuffix = s.substr(firstPartInS);
string secondText = sSuffix + "#" + t;
vector<int> secondPi = solver.getPrefixFunction(secondText);
for (int firstPartInT = 0; firstPartInT <= m; ++firstPartInT) {
int firstPartLength = secondPi[(n - firstPartInS) + firstPartInT];
int secondPartLength = firstPi[m - firstPartInT + firstPartInS];
int indexInS = -1;
if (wasSReversed == false) {
indexInS = firstPartInS - firstPartLength;
}
else {
indexInS = n - firstPartInS - secondPartLength;
}
int indexInT = firstPartInT - secondPartLength;
answer = max(answer, make_tuple(firstPartLength + secondPartLength, indexInS, indexInT));
}
}
return answer;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
cin >> s >> t;
tuple<int, int, int> answer = Solve(s, t, false);
reverse(s.begin(), s.end());
answer = max(answer, Solve(s, t, true));
cout << get<0>(answer) << "\n"
<< get<1>(answer) << " " << get<2>(answer);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |