이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ll long long
#define ui unsigned int
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define oo (1ll << 60)
using namespace std;
const int up = 3003;
const ui base = 31;
const int mod = 2100003007;
unordered_map<ui, int>SubHash;
ui pw[up];
void preClc() {
pw[0] = 1;
for(int i = 1; i < up; ++i) {
pw[i] = base * pw[i - 1];
}
}
string a, b;
void CyclicHashs(ui h, int l, int r) {
int sz = r - l + 1;
SubHash[h] = l;
for(int i = l; i < r - 1; ++i) {
h = h - pw[sz - 1] * ((ui)(a[i] - '`'));
h = base * h + (ui)(a[i] - '`');
SubHash[h] = l;
}
}
void solve() {
preClc();
int n, m;
cin >> a >> b;
bool swaped = false;
if(a.size() > b.size()) {
swap(a, b);
swaped = true;
}
n = (int)a.size();
m = (int)b.size();
/// for all sub-strings store the hash of its lexicographically smallest necklace
for(int i = 0; i < n; ++i) {
ui h = 0;
for(int j = i; j < n; ++j) {
h = base * h + (ui)(a[j] - '`');
CyclicHashs(h, i, j);
}
}
int res = 0;
int sa = 0, sb = 0;
for(int i = 0; i < m; ++i) {
ui h = 0;
for(int j = i; j < m; ++j) {
h = base * h + (ui)(b[j] - '`');
if(SubHash.count(h)) {
if(j - i + 1 > res) {
res = j - i + 1;
sa = SubHash[h];
sb = i;
}
}
}
}
reverse(b.begin(), b.end());
for(int i = 0; i < m; ++i) {
ui h = 0;
for(int j = i; j < m; ++j) {
h = base * h + (ui)(b[j] - '`');
if(SubHash.count(h)) {
if(j - i + 1 > res) {
res = j - i + 1;
sa = SubHash[h];
sb = m - 1 - j;
}
}
}
}
if(swaped) {
swap(sa, sb);
}
cout << res << '\n';
cout << sa << ' ' << sb << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
int t = 1;
while(t--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |