Submission #879719

#TimeUsernameProblemLanguageResultExecution timeMemory
879719leastmerryfishRound words (IZhO13_rowords)C++17
100 / 100
88 ms125588 KiB
#include <bits/stdc++.h> using namespace std; const int mx = 4004; int dp[mx][mx], dir[mx][mx], n, m; string a, b; int solve() { int x, ans = 0; for (int i = 0; i <= n * 2; i++) for (int j = 0; j <= m; j++) { if (i == 0 && j == 0) dp[i][j] = 0, dir[i][j] = -1; else { dp[i][j] = dir[i][j] = -1; if (j > 0 && dp[i][j] < (x = dp[i][j - 1])) dp[i][j] = x, dir[i][j] = 0; if (i > 0 && j > 0 && a[(i - 1) % n] == b[j - 1] && dp[i][j] < (x = dp[i - 1][j - 1] + 1)) dp[i][j] = x, dir[i][j] = 1; if (i > 0 && dp[i][j] < (x = dp[i - 1][j])) dp[i][j] = x, dir[i][j] = 2; } } for (int k=0; k<n; k++) { int i = n + k, j = m; x = 0; while (dir[i][j] != -1) { if (dir[i][j] == 0) j--; else if (dir[i][j] == 1) i--, j--, x++; else i--; } ans = max(ans, x); i = k + 1, j = 0; dir[i][j] = -1; while (j < m) if (dir[i][j + 1] == 0) j++; else { dir[i][j + 1] = 0; if (i++ == n * 2) break; if (dir[i][j + 1] == 1) j++; } } return ans; } int main() { cin >> a >> b; n = (int)a.length(), m = (int)b.length(); int ans = solve(); for (int i=0, j=n-1; i<j; i++, j--) { char tmp = a[i]; a[i] = a[j]; a[j] = tmp; } cout << max(ans, solve()); }
#Verdict Execution timeMemoryGrader output
Fetching results...