// 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;
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;
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()));
}
Compilation message
rowords.cpp: In function 'int main()':
rowords.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s%s", A + 1, B + 1);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
31992 KB |
Output is correct |
2 |
Correct |
34 ms |
31992 KB |
Output is correct |
3 |
Correct |
35 ms |
32120 KB |
Output is correct |
4 |
Correct |
34 ms |
32252 KB |
Output is correct |
5 |
Correct |
35 ms |
32276 KB |
Output is correct |
6 |
Correct |
47 ms |
40696 KB |
Output is correct |
7 |
Correct |
65 ms |
47608 KB |
Output is correct |
8 |
Correct |
100 ms |
47688 KB |
Output is correct |
9 |
Correct |
105 ms |
47744 KB |
Output is correct |
10 |
Correct |
95 ms |
47608 KB |
Output is correct |
11 |
Correct |
96 ms |
49144 KB |
Output is correct |
12 |
Correct |
92 ms |
50808 KB |
Output is correct |
13 |
Correct |
127 ms |
50812 KB |
Output is correct |
14 |
Correct |
107 ms |
49784 KB |
Output is correct |
15 |
Correct |
114 ms |
51288 KB |
Output is correct |
16 |
Correct |
114 ms |
48888 KB |
Output is correct |
17 |
Correct |
91 ms |
50040 KB |
Output is correct |
18 |
Correct |
116 ms |
54136 KB |
Output is correct |
19 |
Correct |
89 ms |
47608 KB |
Output is correct |
20 |
Correct |
126 ms |
51832 KB |
Output is correct |
21 |
Correct |
62 ms |
47608 KB |
Output is correct |
22 |
Correct |
74 ms |
49912 KB |
Output is correct |
23 |
Correct |
85 ms |
51324 KB |
Output is correct |
24 |
Correct |
87 ms |
52344 KB |
Output is correct |
25 |
Correct |
101 ms |
55032 KB |
Output is correct |