#include <bits/stdc++.h>
#define get(arr, x) (((arr[x >> 6] >> (x & 63)) & 1) == 1)
#define set(arr, x) (arr[x >> 6] |= 1llu << (x & 63))
using ulint = unsigned long long;
int lcs(std::string A, std::string B) {
int N = A.size(), M = B.size();
int sz = (M >> 6) + 1;
std::vector<ulint> S[26];
for(int c = 0; c < 26; c++) S[c].resize(sz);
for(int j = 0; j < M; j++) set(S[B[j] - 'a'], j);
std::vector<ulint> row(sz);
for(int j = 0; j < M; j++) if(A[0] == B[j]) { set(row, j); break; }
for(int i = 1; i < N; i++) {
ulint shl_carry = 1;
ulint minus_carry = 0;
for(int k = 0; k < sz; k++) {
// Compute k-th block of x == S[A[i]] OR M[i-1]
ulint x_k = S[A[i] - 'a'][k] | row[k];
// Compute k-th block of "(M[i-1] << 1) | 1"
ulint term_1 = (row[k] << 1) | shl_carry;
shl_carry = row[k] >> 63;
// Compute k-th block of "x - ((M[i-1] << 1) | 1)"
auto sub_carry = [](ulint &x, ulint y){
ulint tmp = x;
return (x = tmp - y) > tmp;
};
ulint term_2 = x_k;
minus_carry = sub_carry(term_2, minus_carry);
minus_carry += sub_carry(term_2, term_1);
row[k] = x_k & (x_k ^ term_2);
}
row[M >> 6] &= (1llu << (M & 63)) - 1;
}
int ret = 0;
for(int j = 0; j < M; j++) if(get(row, j)) ret += 1;
return ret;
}
int main() {
std::string a, b;
std::cin >> a >> b;
int ans = 0;
for(int rep = 0; rep < 2; rep++) {
for(int i = 0; i < int(b.size()); i++) {
ans = std::max(ans, lcs(a, b));
std::rotate(b.begin(), b.begin() + 1, b.end());
}
std::reverse(b.begin(), b.end());
}
std::cout << ans << std::endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
115 ms |
376 KB |
Output is correct |
8 |
Correct |
118 ms |
376 KB |
Output is correct |
9 |
Correct |
115 ms |
376 KB |
Output is correct |
10 |
Correct |
111 ms |
256 KB |
Output is correct |
11 |
Correct |
120 ms |
364 KB |
Output is correct |
12 |
Correct |
146 ms |
504 KB |
Output is correct |
13 |
Correct |
145 ms |
504 KB |
Output is correct |
14 |
Correct |
125 ms |
376 KB |
Output is correct |
15 |
Correct |
163 ms |
376 KB |
Output is correct |
16 |
Correct |
121 ms |
256 KB |
Output is correct |
17 |
Correct |
54 ms |
368 KB |
Output is correct |
18 |
Correct |
120 ms |
376 KB |
Output is correct |
19 |
Correct |
111 ms |
364 KB |
Output is correct |
20 |
Correct |
109 ms |
256 KB |
Output is correct |
21 |
Correct |
9 ms |
256 KB |
Output is correct |
22 |
Correct |
17 ms |
256 KB |
Output is correct |
23 |
Correct |
29 ms |
256 KB |
Output is correct |
24 |
Correct |
31 ms |
400 KB |
Output is correct |
25 |
Correct |
51 ms |
376 KB |
Output is correct |