First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100.
For each case, output k – the length of a longest common subsequence in one line.
abcd cxbydz
2
#include <stdio.h> #include<string.h> #define MAX 100 int main() { char s1[MAX], s2[MAX]; int dp[MAX][MAX]; scanf("%s", s1); scanf("%s", s2); int len1 = strlen(s1); int len2 = strlen(s2); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = (dp[i][j - 1] - dp[i - 1][j] > 0) ? dp[i][j - 1] : dp[i - 1][j]; } } printf("%d", dp[len1][len2]); return 0; }