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

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →