#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool cmax(X &a, const Y &b) {
return a < b ? a = b, 1 : 0;
}
const int N = 2005;
int dp[N][N], dir[N][N], m, n;
/// 0: up, 1: up-left, 2:left
void init_lcs(string s, string t) {
memset(dp, 0, sizeof dp);
memset(dir, 0, sizeof dp);
m = s.size(), n = t.size(); t += t;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= 2 * n; j++) {
dp[i][j] = dp[i - 1][j];
if (s[i - 1] == t[j - 1] && cmax
(dp[i][j], dp[i - 1][j - 1] + 1))
dir[i][j] = 1;
if (cmax(dp[i][j], dp[i][j - 1]))
dir[i][j] = 2;
}
}
int get_lcs(int i, int j) {
int lcs = 0;
while (i) {
if (dir[i][j] == 2) j--;
else if (dir[i][j] == 1) {
i--; j--; lcs++;
}
else i--;
}
return lcs;
}
void remove_col(int j) {
int i = 0;
while (j < 2 * n) {
if (dir[i][j + 1] == 2)
dir[i][++j] = 0;
else {
if (i == m) break;
if (dir[++i][j + 1] == 1)
dir[i][++j] = 0;
}
}
}
int circular_lcs(string s, string t) {
init_lcs(s, t); int lcs = 0;
for (int j = 0; j < n; j++) {
cmax(lcs, get_lcs(m, j + n));
remove_col(j);
}
return lcs;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
string s, t; cin >> s >> t;
cout << circular_lcs(s, t) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
31852 KB |
Output isn't correct |
2 |
Correct |
17 ms |
31852 KB |
Output is correct |
3 |
Correct |
17 ms |
31852 KB |
Output is correct |
4 |
Correct |
17 ms |
31852 KB |
Output is correct |
5 |
Correct |
16 ms |
31852 KB |
Output is correct |
6 |
Correct |
18 ms |
31852 KB |
Output is correct |
7 |
Correct |
39 ms |
31852 KB |
Output is correct |
8 |
Incorrect |
59 ms |
31852 KB |
Output isn't correct |
9 |
Correct |
46 ms |
31872 KB |
Output is correct |
10 |
Correct |
41 ms |
31852 KB |
Output is correct |
11 |
Correct |
42 ms |
31872 KB |
Output is correct |
12 |
Incorrect |
35 ms |
31864 KB |
Output isn't correct |
13 |
Incorrect |
63 ms |
31980 KB |
Output isn't correct |
14 |
Runtime error |
64 ms |
64236 KB |
Execution killed with signal 11 |
15 |
Runtime error |
66 ms |
64240 KB |
Execution killed with signal 11 |
16 |
Incorrect |
52 ms |
31852 KB |
Output isn't correct |
17 |
Incorrect |
35 ms |
31780 KB |
Output isn't correct |
18 |
Correct |
48 ms |
31852 KB |
Output is correct |
19 |
Correct |
37 ms |
31852 KB |
Output is correct |
20 |
Incorrect |
52 ms |
31852 KB |
Output isn't correct |
21 |
Correct |
22 ms |
31852 KB |
Output is correct |
22 |
Correct |
26 ms |
31852 KB |
Output is correct |
23 |
Correct |
32 ms |
31852 KB |
Output is correct |
24 |
Incorrect |
32 ms |
31872 KB |
Output isn't correct |
25 |
Incorrect |
37 ms |
31852 KB |
Output isn't correct |