Submission #826482

#TimeUsernameProblemLanguageResultExecution timeMemory
826482QwertyPiNecklace (Subtask 1-3) (BOI19_necklace1)C++14
25 / 85
1560 ms284 KiB
#include <bits/stdc++.h> #define int long long using namespace std; struct answer{ int a, x, y; }; const int P = 1e9 + 7, A = 13895; int norm(int x){ return (x % P + P) % P; } bool are_equal(const string& s, const string& t){ if(s.length() != t.length()) return false; int n = s.size(); int h_s = 0; for(int i = 0; i < n; i++) h_s = (h_s * A + s[i] - 'a') % P; int pw_n = 1; for(int i = 0; i < n; i++) pw_n = pw_n * A % P; int h_t[n * 2 + 1]; h_t[0] = 0; for(int i = 0; i < n * 2; i++) h_t[i + 1] = (h_t[i] * A + t[i % n] - 'a') % P; for(int i = 0; i < n; i++){ if(h_s == norm(h_t[i + n] - h_t[i] * pw_n)){ return true; } } return false; } answer query(const string& s, const string& t){ int n = s.size(), m = t.size(); answer ans {-1, 0, 0}; for(int l = 1; l <= min(n, m); l++){ for(int i = 0; i <= n - l; i++){ for(int j = 0; j <= m - l; j++){ bool ok = are_equal(s.substr(i, l), t.substr(j, l)); if(ok){ ans = answer {l, i, j}; } } } } return ans; } int32_t main(){ string s, t; cin >> s >> t; answer a1 = query(s, t); reverse(t.begin(), t.end()); answer a2 = query(s, t); a2.y = t.size() - (a2.y + a2.a); answer ans; ans.a = -1; if(a1.a > ans.a) ans = a1; if(a2.a > ans.a) ans = a2; cout << ans.a << endl; cout << ans.x << ' ' << ans.y << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...