# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1056998 | albinoATojuz | Round words (IZhO13_rowords) | C++14 | 17 ms | 21868 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;
class Solution {
public:
int longestCommonSubsequence(string a, string b) {
int dp[a.size()][b.size()];
for (int i = 0; i < a.size(); i++) { fill(dp[i], dp[i] + b.size(), 0); }
for (int i = 0; i < a.size(); i++) {
if (a[i] == b[0]) dp[i][0] = 1;
if (i != 0) dp[i][0] = max(dp[i][0], dp[i - 1][0]);
}
for (int i = 0; i < b.size(); i++) {
if (a[0] == b[i]) dp[0][i] = 1;
if (i != 0) dp[0][i] = max(dp[0][i], dp[0][i - 1]);
}
for (int i = 1; i < a.size(); i++) {
for (int j = 1; j < b.size(); j++) {
if (a[i] == b[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[a.size() - 1][b.size() - 1];
}
};
int main () {
string a,b,c,d;
cin >> a >> b;
c = a + a;
d = b + b;
Solution s;
cout << s.longestCommonSubsequence(c,d) - s.longestCommonSubsequence(a,b) << "\n";
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |