LCS Returns


Problem Statement :


Given two strings, a and b, find and print the total number of ways to insert a character at any position in string a such that the length of the Longest Common Subsequence of characters in the two strings increases by one.

Input Format

The first line contains a single string denoting a.
The second line contains a single string denoting b.

Constraints

Scoring

1 <= |a|,|b| <= 5000
Strings a and b are alphanumeric (i.e., consisting of arabic digits and/or upper and lower case English letters).
The new character being inserted must also be alphanumeric (i.e., a digit or upper/lower case English letter).

Subtask

1 <= |a|,|b| <= 1000 for 66.7% of the maximum score.
Output Format

Print a single integer denoting the total number of ways to insert a character into string a in such a way that the length of the longest common subsequence of a and b increases by one.



Solution :



title-img


                            Solution in C :

In C++ :





#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

char a[6000], b[6000];
int la, lb;
int Pref[5005][5005], Suff[5005][5005];
bool Can[256];

int main() {
    cin >> (a + 1) >> (b + 1);
    la = strlen(a + 1);
    lb = strlen(b + 1);

    for(int i = 1; i <= la; ++i)
        for(int j = 1; j <= lb; ++j) {
            if(a[i] == b[j]) {
                Pref[i][j] = 1 + Pref[i - 1][j - 1];
            } else {
                Pref[i][j] = max(Pref[i - 1][j], Pref[i][j - 1]);
            }
    }
    
    for(int i = la; i >= 1; --i)
        for(int j = lb; j >= 1; --j) {
            if(a[i] == b[j])
                Suff[i][j] = 1 + Suff[i + 1][j + 1];
            else
                Suff[i][j] = max(Suff[i + 1][j], Suff[i][j + 1]);
    }
    
    int best = Pref[la][lb];
    int cnt = 0;
    
    for(int i = 1; i <= la + 1; ++i) {
        memset(Can, 0, sizeof(Can));
        for(int j = 1; j <= lb; ++j) {
            if(Can[b[j]]) continue;
            
            if(Pref[i - 1][j - 1] + Suff[i][j + 1] == best) {
                Can[b[j]] = 1;
                cnt += 1;
            }
        }
    }
    
    cout << cnt << endl;
    
    return 0;
}








In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] a = in.next().toCharArray();
        char[] b = in.next().toCharArray();
        int n = a.length;
        int m = b.length;
        int[][] dp = new int[n+2][m+2];
        int[][] dp2 = new int[n+2][m+2];
        for (int i=1; i<=n; i++) {
            for (int j=1; j<=m; j++) {
                if (a[i-1] == b[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j-1], dp[i-1][j]);
                }
            }
        }
        int r = dp[n][m], ans = 0;
        
        for (int i=n; i>=1; i--) {
            for (int j=m; j>=1; j--) {
                if (a[i-1] == b[j-1]) {
                    dp2[i][j] = dp2[i+1][j+1] + 1;
                } else {
                    dp2[i][j] = Math.max(dp2[i][j+1], dp2[i+1][j]);
                }
            }
        }
        
        for (int i=0; i<=n; i++) {
            Set<Character> set = new HashSet<>();
            for (int j=1; j<=m; j++) if (!set.contains(b[j-1])) {
                int v = dp[i][j-1] + dp2[i+1][j+1];
                //System.out.print(v+" ");
                if (v == r) {
                    ans++;
                    set.add(b[j-1]);
                }
            }
          //  System.out.println();
        }
        
        System.out.println(ans);
    }
}








In C :





#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int d1[5002][5002];
int d2[5002][5002];
char s1[6000],s2[6000];
int cc[256];

int main() {
    int i,j,l1,l2,p,c,ret=0;
    scanf("%s %s",s1,s2);
    l1=strlen(s1),l2=strlen(s2);
    for(i=1;i<=l1;i++) for(j=1;j<=l2;j++) {
        d1[i][j]=d1[i-1][j];
        if (d1[i][j-1]>d1[i][j]) d1[i][j]=d1[i][j-1];
        if (s1[i-1]==s2[j-1]) d1[i][j]=d1[i-1][j-1]+1;
    }
    for(i=l1-1;i>=0;i--) for(j=l2-1;j>=0;j--) {
        d2[i][j]=d2[i+1][j];
        if (d2[i][j+1]>d2[i][j]) d2[i][j]=d2[i][j+1];
        if (s1[i]==s2[j]) d2[i][j]=d2[i+1][j+1]+1;
    }
    for(i=0;i<=l1;i++) {
        for(j=0;j<l2;j++) if (d1[i][j]+d2[i][j+1]==d1[l1][l2]) cc[s2[j]]=1;
        for(j=0;j<128;j++) if (cc[j]) ret++,cc[j]=0;
    }
    printf("%d\n",ret);
    return 0;
}








In Python3 :






import string

#calculates longest common subsequence of 2 strings
def lcs(s1, s2):
    n = len(s1)
    m = len(s2)
    
    dp = [[0 for _ in range(m+1)] for _ in range(n+1)]
    
    for i in range(n):
        for j in range(m):
            if (s1[i] == s2[j]):
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])
    
    return dp

#returns array of indexes from string 2 for each alphabet letter
def alpIndexes(s, alp):
    d = dict()
    for letter in alp:
        d[letter] = []
        
    for i in range(len(s)):
        d[s[i]].append(i)
        
    return d

#main
s1 = input().strip()
s2 = input().strip()
n = len(s1)
m = len(s2)
chars = list(string.ascii_lowercase) #lowercase alphabet letters array
charIndexes = alpIndexes(s2, chars)

dpl = lcs(s1, s2)
dpr = lcs(s1[::-1], s2[::-1])
lcs = dpl[n][m]
#print(dpl)
#print(dpr)
ans = 0
for i in range(0, n+1):
    for letter in chars:
        for j in charIndexes[letter]:
            if (dpl[i][j] + dpr[n-i][m-j-1] == lcs):
                ans += 1
                break
        
print(ans)
                        








View More Similar Problems

QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →

Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

View Solution →

Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

View Solution →