# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
97466 | 2019-02-16T11:06:21 Z | Kastanda | 원형 문자열 (IZhO13_rowords) | C++11 | 0 ms | 0 KB |
// I do it for the glory #include<bits/stdc++.h> using namespace std; const int N = 2009, NN = N * 2; int n, m, dp[NN][N], P[NN][N]; char A[NN], B[N]; inline int RLCS() { int Mx = 0;n memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { dp[i][j] = max({dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + (A[i] == B[j])}); if (dp[i][j] == dp[i][j - 1]) P[i][j] = 2; else if (A[i] == B[j] && dp[i][j] == dp[i - 1][j - 1] + 1) P[i][j] = 1; else P[i][j] = 0; } Mx = dp[n][m]; for (int i = n + 1; i <= n + n; i++) { /* **** Deleting the first row **** */ int last = i - n, nw = 0; for (int j = 1; j <= m; j++) { if (last != i && P[last][j] != 2) { P[last][j] = 2; last ++; while (last < i && P[last][j] == 0) P[last][j] = 2, last ++; } if (last == i) dp[i - 1][j] --; } /* **** Adding the last row **** */ for (int j = 1; j <= m; j++) { dp[i][j] = max({dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + (A[i] == B[j])}); if (dp[i][j] == dp[i][j - 1]) P[i][j] = 2; else if (A[i] == B[j] && dp[i][j] == dp[i - 1][j - 1] + 1) P[i][j] = 1; else P[i][j] = 0; } Mx = max(Mx, dp[i][m]); } return (Mx); } int main() { scanf("%s%s", A + 1, B + 1); n = strlen(A + 1); m = strlen(B + 1); memcpy(A + n + 1, A + 1, n); int Mx = RLCS(); reverse(B + 1, B + m + 1); return !printf("%d\n", max(Mx, RLCS())); }