#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "rowords"
void Fi(){
if(fopen(PROB".in", "r")){
freopen(PROB".in", "r", stdin);
freopen(PROB".out", "w", stdout);
}
}
const int N = 2e3 + 1;
int dp[N * 2][N], t[N * 2][N];
int n, m, ans;
string s, p;
const int dx[] = {-1, 0, -1};
const int dy[] = {0, -1, -1};
void calc(){
memset(dp, 0, sizeof(dp));
memset(t, -1, sizeof(t));
FORU(i, 1, n * 2) t[i][0] = 0;
FORU(j, 1, m) t[0][j] = 1;
FORU(i, 1, n * 2) FORU(j, 1, m){
auto selection = {
dp[i - 1][j],
dp[i][j - 1],
dp[i - 1][j - 1] + (s[i] == p[j])
};
dp[i][j] = *max_element(ALL(selection));
t[i][j] = max_element(ALL(selection)) - selection.begin();
}
ans = max(ans, dp[n][m]);
FORU(row, 1, n){
{
int i = row, j;
for(j = 1; j <= m && t[i][j] != 2; ++j);
if(j <= m){
t[i][j] = 1;
while(i < n * 2 && j <= m){
if(t[i + 1][j] == 0) t[++i][j] = 1;
else if(j == m) break;
else if(t[i + 1][j + 1] == 2) t[++i][++j] = 1;
else ++j;
}
}
}
{
int tmp = 0;
for(int i = n + row, j = m; i > row && j > 0;){
int d = t[i][j];
if(d == 2) ++tmp;
i += dx[d], j += dy[d];
}
ans = max(ans, tmp);
}
}
}
int main(){
IOS;
Fi();
cin >> s >> p;
n = s.size(), m = p.size();
s = " " + s + s;
p = " " + p;
calc();
reverse(p.begin() + 1, p.begin() + m + 1);
calc();
cout << ans;
return 0;
}
Compilation message
rowords.cpp: In function 'void Fi()':
rowords.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen(PROB".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
rowords.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(PROB".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
62924 KB |
Output is correct |
2 |
Correct |
35 ms |
62892 KB |
Output is correct |
3 |
Correct |
33 ms |
62924 KB |
Output is correct |
4 |
Correct |
34 ms |
62924 KB |
Output is correct |
5 |
Incorrect |
38 ms |
62880 KB |
Output isn't correct |
6 |
Correct |
52 ms |
62960 KB |
Output is correct |
7 |
Correct |
67 ms |
62968 KB |
Output is correct |
8 |
Incorrect |
107 ms |
62924 KB |
Output isn't correct |
9 |
Incorrect |
109 ms |
62980 KB |
Output isn't correct |
10 |
Incorrect |
122 ms |
62984 KB |
Output isn't correct |
11 |
Incorrect |
107 ms |
62992 KB |
Output isn't correct |
12 |
Correct |
110 ms |
62988 KB |
Output is correct |
13 |
Incorrect |
150 ms |
62884 KB |
Output isn't correct |
14 |
Incorrect |
120 ms |
62956 KB |
Output isn't correct |
15 |
Incorrect |
133 ms |
62992 KB |
Output isn't correct |
16 |
Incorrect |
133 ms |
62984 KB |
Output isn't correct |
17 |
Incorrect |
121 ms |
62924 KB |
Output isn't correct |
18 |
Incorrect |
150 ms |
62988 KB |
Output isn't correct |
19 |
Incorrect |
97 ms |
62924 KB |
Output isn't correct |
20 |
Incorrect |
156 ms |
62992 KB |
Output isn't correct |
21 |
Incorrect |
109 ms |
62984 KB |
Output isn't correct |
22 |
Incorrect |
129 ms |
62988 KB |
Output isn't correct |
23 |
Incorrect |
142 ms |
62936 KB |
Output isn't correct |
24 |
Incorrect |
171 ms |
62924 KB |
Output isn't correct |
25 |
Incorrect |
174 ms |
63044 KB |
Output isn't correct |