Mini 4th Sem Algo
Mini 4th Sem Algo
Submitted by
RAMALAKSHMI K (953622104080)
BACHELOR OF ENGINEERING
IN
RAJAPALAYAM
JUNE 2024
CS3401 - ALGORITHMS
PAGE \* MERGEFORMAT 2
MINIMISE LCS
PROBLEM STATEMENT
Chef is given two strings AA and BB of length NN containing lowercase
English letters.
Chef can rearrange both the strings in any way he wants. He wants to minimize
the length of LCS (Longest Common Subsequence) of both the strings.
Find the minimum length of LCS of AA and BB if he optimally rearranges both
the strings.
INPUT FORMAT
The first line of input will contain a single integer TT, denoting the
number of test cases.
Each test case consists of multiple lines of input.
o The first line of each test case contains an integer NN -
denoting the length of AA and BB.
o The second line of each test case contains a string AA.
o The third line of each test case contains a string BB.
OUTPUT FORMAT
For each test case, output the minimum length of LCS Chef can obtain.
Constraints
1≤𝑇≤10001≤T≤1000
1≤𝑁≤1051≤N≤105
𝐴A and 𝐵B contain lowercase English letters
The sum of 𝑁N over all test cases does not exceed 2⋅1052⋅105.
Sample 1:
Input
Output
3
PAGE \* MERGEFORMAT 2
4
aaaa
aaab
1
c
z
5
abcde
cdefg
3
0
1
Explanation:
Test case 1: LCS(aaaa,aaab)=aaaLCS(aaaa,aaab)=aaa. It can be shown that no
other rearrangement of the strings produces a smaller LCS.
Test case 3: LCS(acbed,gdefc)=cLCS(acbed,gdefc)=c. Note that multiple
longest common subsequences exist for this arrangement. It can be shown that
no other rearrangement of the strings produces a smaller LCS.
AIM:
The project aims to minimize the length of the Longest Common Subsequence
(LCS) between two strings AAA and BBB across multiple test cases. It
achieves this by rearranging both strings optimally.
DESCRIPTION:
INPUT READING:
Read the number of test cases TTT.
For each test case, read the length NNN of strings AAA and BBB, followed by
the strings themselves.
CHARACTER FREQUENCY COUNTING:
PAGE \* MERGEFORMAT 2
Use arrays s and a of size 27 (to account for 'a' to 'z') to count occurrences of
each character in strings AAA and BBB, respectively.
OPTIMAL LCS CALCULATION:
Initialize variables to store the minimum LCS length.
Iterate over each character ('a' to 'z'):
Determine the minimum count of each character that appears in both strings
AAA and BBB.
Accumulate these minimum counts to find the smallest possible LCS length.
CODE:
#include <stdio.h>
int main(void) {
int Testcase;
scanf("%d",&Testcase);
while(Testcase--)
int N,c=0,i,k;
scanf("%d",&N);
char str1[N+1],str2[N+1];
int s[27]={0},a[27]={0};
scanf("%s%s",str1,str2);
for(i=0;i<N;i++)
s[str1[i]-'a']++;
for(i=0;i<N;i++)
a[str2[i]-'a']++;
for(i=0;i<27;i++)
{
if(s[i]>0&&a[i]>0)
{
if(s[i]>=a[i])
PAGE \* MERGEFORMAT 2
{
k=a[i];
if(k>=c)
c=k;
}
else{
k=s[i];
if(k>=c)
c=k;
}
}}
printf("%d\n",c);
}
return 0;
}
OUTPUT:
PAGE \* MERGEFORMAT 2
CONCLUSION:
This project minimizes the Longest Common Subsequence (LCS) length
between pairs of strings AAA and BBB by rearranging characters strategically.
It counts character frequencies, computes the smallest possible LCS length
across multiple test cases, and ensures efficient performance within specified
constraints. By systematically comparing minimum character counts.
PAGE \* MERGEFORMAT 2