#include<bits/stdc++.h>
#include<unordered_map>
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
#define rrep(i,a,b) for(int i=int(a);i>int(b);i--)
#define trav(a,v) for(auto& a: v)
#define sz(v) v.size()
#define all(v) v.begin(),v.end()
#define vi vector<int>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const long long inf = 1e15;
vector<ull> suffix(1e5);
vector<ull> suffix2(1e5);
vector<ull> suffix3(1e5);
vector<ull> powers(1e5);
ll n;
ull getS(vector<ull>& v,ll start, ll end) {
return v[start] - (v[end] * powers[end-start]);
}
int main() {
cin.sync_with_stdio(false);
vector<ull> primes = { 43,47,53,59,61,67,71,73,79,83,89,97,101,103,107 };
string a,b;
cin >> a >> b;
n = a.size();
ull c = primes[rand() % primes.size()];
powers[0] = 1;
rep(i, 1, 1e5)powers[i] = powers[i - 1] * c;
rrep(i, a.size() - 1, -1)suffix[i] = a[i] + c * suffix[i + 1];
rrep(i, b.size() - 1, -1)suffix2[i] = b[i] + c * suffix2[i + 1];
reverse(all(a));
rrep(i, a.size() - 1, -1)suffix3[i] = a[i] + c * suffix3[i + 1];
unordered_map<ull, int> mp;
rep(i, 0, a.size()+1) {
rep(j, i + 1, a.size() + 1) {
mp[getS(suffix, i, j)] = i+1;
mp[getS(suffix3, i, j)] = a.size()-i-(j-i)+1;
}
}
ll l = 1, r = a.size();
ll ans = 0;
ll pos1, pos2;
rep (mid,0,b.size()) {
int done = 0;
if (a.size() > 200 && mid > b.size() / 2)break;
rep(i, 0, b.size() - mid + 1) {
ull val = getS(suffix2, i, i + mid);
rep(j, 0, mid) {
val -= getS(suffix2, i+mid - j - 1, i+mid - j) * powers[mid-1];
val *= c;
val += getS(suffix2, i+mid - j - 1, i+mid - j);
if (mp[val]) {
done = mp[val];
pos2 = i;
break;
}
}
}
if (done) {
ans = mid;
pos1 = done;
}
}
cout << ans << endl;
if(ans)cout << pos1-1 << " " << pos2 << endl;
return 0;
}
Compilation message
necklace.cpp: In function 'int main()':
necklace.cpp:48:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | if (a.size() > 200 && mid > b.size() / 2)break;
| ~~~~^~~~~~~~~~~~~~
necklace.cpp:43:5: warning: unused variable 'l' [-Wunused-variable]
43 | ll l = 1, r = a.size();
| ^
necklace.cpp:43:12: warning: unused variable 'r' [-Wunused-variable]
43 | ll l = 1, r = a.size();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5780 KB |
Output is correct |
2 |
Correct |
15 ms |
6108 KB |
Output is correct |
3 |
Correct |
17 ms |
5772 KB |
Output is correct |
4 |
Correct |
15 ms |
6152 KB |
Output is correct |
5 |
Correct |
57 ms |
9980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5780 KB |
Output is correct |
2 |
Correct |
15 ms |
6108 KB |
Output is correct |
3 |
Correct |
17 ms |
5772 KB |
Output is correct |
4 |
Correct |
15 ms |
6152 KB |
Output is correct |
5 |
Correct |
57 ms |
9980 KB |
Output is correct |
6 |
Partially correct |
594 ms |
54388 KB |
Output is partially correct |
7 |
Partially correct |
1128 ms |
91172 KB |
Output is partially correct |
8 |
Correct |
272 ms |
26612 KB |
Output is correct |
9 |
Execution timed out |
1582 ms |
165120 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
5780 KB |
Output is correct |
2 |
Correct |
15 ms |
6108 KB |
Output is correct |
3 |
Correct |
17 ms |
5772 KB |
Output is correct |
4 |
Correct |
15 ms |
6152 KB |
Output is correct |
5 |
Correct |
57 ms |
9980 KB |
Output is correct |
6 |
Partially correct |
594 ms |
54388 KB |
Output is partially correct |
7 |
Partially correct |
1128 ms |
91172 KB |
Output is partially correct |
8 |
Correct |
272 ms |
26612 KB |
Output is correct |
9 |
Execution timed out |
1582 ms |
165120 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |