Conversation with Merlin [email protected] · Fri Dec 08 2023

Shyam is very curious about strings and luckily, today's topic in his coding class is strings!

He learned about strings, sub strings and sub sequences. After the class, Shyam's interest towards strings is doubled. So, he asked his mentor to assign him a string based activity. Then, the mentor gave him two strings S1 and S2, and another integer K. Shyam is allowed to select one of the strings and perform an operation i.e., he can select a letter and replace it with any of the neighboring K letters. He can perform this operation on any number of letters in the selected string, but for a selected letter this operation can be performed exactly once.

For example, if the letter he selected is 'p' and K=2, then he can replace 'p' with letters 'n','o','q','r'. Suppose if he choose the letter 'a' and K=1, then he can replace it with letters 'z','b'. Consider z and a to be adjacent.

Given S1, S2 and K, find the maximum length of the common sub sequence of these two strings after performing operations on any one of the two strings.

Constraints 1 <= len(S1), len(S2) <= 10^3

0 <= K <= 10

S1, S2 comprises of lower case letters only.

Input First line consists of the string, S1.

Second line consists of the string, S2.

Third line consists of an integer K.

Output Print the maximum possible length of the common sub sequence of these two strings after performing operations on any one of the two strings.

Time Limit (secs) 1

Examples Example 1

Input

australia

yqxtuzmvbk

1

Output

5

Explanation

If we modify S1 as "atsurzmib" then the longest possible sub sequence is "tuzmb" and its length is 5. No other replacements will give sub sequence of length greater than 5.

Example 2

Input

abcde

ldeqf

2

Output

3

Explanation

If we modify S2 as "lcdqe" then the longest possible sub sequence is "cde" and its length is 3. No other replacements will give sub sequence of length greater than 3 Write a C program

Here's a C program to solve the problem. This program calculates the maximum length of the common subsequence of the two given strings after performing operations on any one of the two strings.

#include <stdio.h> #include <string.h> int max(int, int b) { return (a > b) a : b; } int min(int a, int b) { return (a < b) ? a : b; } int dp[1005][5][11]; int max_common_subsequence(char s1 char s2[], int k) { int len1 =(s1); int len2 = strlen(s2); (int i = 0; i <= len1; i++) { for (int j = 0; j <=2; j++) { for (int p = ; p <= k; p++) { if (i == 0 || j == 0) { dp[i][j][p] 0; } else if (s1[i -1] == s2[j - 1]) { dp[i][j][p] = 1 + dp[i - ][j - 1][p]; } else { dp[i][j][p] = max(dp[i - ][j][p], dp[i][j - 1][]); } if (p > 0 && s1[i - 1] != s2[j - ]) { dp[i][j][p] = max(dp[i][j][p 1 + dp[i - 1][j - 1][p - 1]); } } } } return dp[len1][len2][k]; } int main() { char1[1005], s2[1005]; int k; scanfs", s1); scanf("%s", s2); scanf("%d", &k); int result = max_common(s1, s2, k); printf("%d\n result); return 0; }

You can this program to find the maximum possible length of the common subsequence of the two strings after performing operations on any one of the two strings.

I hope this helps! Let me know if you have further questions need additional assistance.