# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
483457 | XII | Round words (IZhO13_rowords) | C++17 | 178 ms | 63000 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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[] = {0, -1, -1};
const int dy[] = {-1, -1, 0};
void calc(){
memset(dp, 0, sizeof(dp));
memset(t, -1, sizeof(t));
FORU(i, 1, n * 2) t[i][0] = 2;
FORU(j, 1, m) t[0][j] = 0;
FORU(i, 1, n * 2) FORU(j, 1, m){
auto selection = {
dp[i][j - 1],
dp[i - 1][j - 1] + (s[i] == p[j]),
dp[i - 1][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] != 1; ++j);
if(j <= m){
t[i][j] = 0;
while(i < n * 2 && j <= m){
if(t[i + 1][j] == 2) t[++i][j] = 0;
else if(j == m) break;
else if(t[i + 1][j + 1] == 1) t[++i][++j] = 0;
else ++j;
}
}
}
{
int tmp = 0;
for(int i = n + row, j = m; i > row && j > 0;){
int d = t[i][j];
if(d == 1) ++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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |