#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];
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;
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: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 |
20 ms |
6544 KB |
Output is correct |
2 |
Correct |
35 ms |
7552 KB |
Output is correct |
3 |
Correct |
44 ms |
6736 KB |
Output is correct |
4 |
Correct |
35 ms |
7580 KB |
Output is correct |
5 |
Correct |
82 ms |
13628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6544 KB |
Output is correct |
2 |
Correct |
35 ms |
7552 KB |
Output is correct |
3 |
Correct |
44 ms |
6736 KB |
Output is correct |
4 |
Correct |
35 ms |
7580 KB |
Output is correct |
5 |
Correct |
82 ms |
13628 KB |
Output is correct |
6 |
Execution timed out |
1570 ms |
95548 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6544 KB |
Output is correct |
2 |
Correct |
35 ms |
7552 KB |
Output is correct |
3 |
Correct |
44 ms |
6736 KB |
Output is correct |
4 |
Correct |
35 ms |
7580 KB |
Output is correct |
5 |
Correct |
82 ms |
13628 KB |
Output is correct |
6 |
Execution timed out |
1570 ms |
95548 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |