Най-дълга обща подредица
Дадени са две числови редици:
// lsc.c
#include <stdio.h>
#include <string.h>
#define MAXN 100
#define MAX(a,b) (((a)>(b)) ? (a) : (b))
char F[MAXN][MAXN];
const char x[MAXN] = "acbcacbcaba";
const char y[MAXN] = "abacacacababa";
unsigned m,n;
/*
"acb cacbcaba " "a cbcac bcaba"
"a bacacacababa" "abacacacab aba"
solution "a b cac caba "
"a c cac b aba"
*/
unsigned lcs_len(void)
{ unsigned i,j;
for (i=0; i<=m; i++) F[i][0]
= 0;
for (j=0; j<=n; j++) F[0][j]
= 0;
for (i=1; i<=m; i++)
for (j=1; j<=n;
j++)
if (x[i-1] == y[j-1]) F[i][j]=F[i-1][j-1]+1;
else F[i][j] = MAX(F[i-1][j], F[i][j-1]);
return F[m][n];
}
void print(unsigned i, unsigned j)
{ if (i==0 || j==0) return;
if (x[i-1] == y[j-1])
{ print(i-1,j-1);
printf("%c",
x[i-1]);
}
else if (F[i][j] == F[i-1][j])
print(i-1,j);
else print(i,j-1);
}
int main()
{ m = strlen(x); n = strlen(y);
printf("%u\n", lcs_len());
print(m,n);
return 0;
}
9
accacbaba |
Сравнение на символни низове
Дадени са два символни низа s1
и s2. Задачата е от първия низ s1 да
се получи втория низ s2, като се използват операциите:
- replace(i, x)- замества i-тия
символ на s1 със символа x;
- insert(i, x) - вмъква символа x
на позиция i в s1;
- delete(i) - изтрива i-тия символ
на s1.
За всяка от трите операции е зададена цена. Търсим крайна редица от
операции за решаване на задачата, която е с минимална цена.
Нека F(i, j) е минималната цената
за уеднаквяване на началните последователности (префикси)
Ако s1[i] = s2[j], тов противен случай ще се вземе минималното от трите числа:F(i, j) = F(i - 1, j - 1),
Остава да се пресметнат стойностите на F(i, 0) и F(0, j). F(i, 0) означава празен втори низ, следователно F(i, 0) = i cd. Аналогично при празен първи низ получаваме F(0, j) = j ci.F(i - 1, j - 1) + cr
F(i, j - 1) + ci
F(i - 1, j) + cd
// trans.c
#include <stdio.h>
#include <string.h>
#define MAX 100
#define min2(a,b) (((a)<(b)) ? (a)
: (b))
#define min3(a,b,c) (min2(min2(a,b),(c)))
#define COST_DELETE 1
#define COST_INSERT 2
#define COST_REPLACE(i,j) ((s1[i] == s2[j])
? 0 : 3)
unsigned F[MAX+1][MAX+1];
unsigned n1, n2;
const char *s1 = "_abracadabra";
const char *s2 = "_mabragabra";
unsigned editDistance(void)
{ unsigned i,j;
for (i=0; i<=n1; i++) F[i][0]
= i*COST_DELETE;
for (j=0; j<=n2; j++) F[0][j]
= j*COST_INSERT;
for (i=1; i<=n1; i++)
for (j=1; j<=n2;
j++)
F[i][j] = min3(F[i-1][j-1] + COST_REPLACE(i,j),
F[i ][j-1] + COST_INSERT,
F[i-1][j ] + COST_DELETE);
return F[n1][n2];
}
void printEditOperations(unsigned i, unsigned
j)
{
if (j==0) for (j=1; j<=i; j++)
printf("DELETE(%u) ",j);
else if (i==0) for (i=1; i<=j;
i++) printf("INSERT(%u, %c) ",i, s2[i]);
else if (i>0 && j>0)
{ if ( F[i][j] == F[i-1][j-1] +
COST_REPLACE(i,j) )
{ printEditOperations(i-1,j-1);
if (COST_REPLACE(i,j)>0)
printf("REPLACE(%u, %c) ", i, s2[j]);
}
else if (F[i][j] == F[i][j-1]
+ COST_INSERT)
{ printEditOperations(i,j-1);
printf("INSERT(%u,
%c) ", i, s2[j]);
}
else if (F[i][j] == F[i-1][j]
+ COST_DELETE)
{ printEditOperations(i-1,j);
printf("DELETE(%u)
", i);
}
}
}
int main()
{ n1 = strlen(s1)-1; n2 = strlen(s2)-1;
printf("Distance: %u\n", editDistance());
printEditOperations(n1,n2);
printf("\n");
return 0;
}
Distance: 7
INSERT(1, m) DELETE(4) DELETE(5) REPLACE(7, g) |
_abracadabra | |||
INSERT(1, m) | 2 |
012345678901
_abracadabra |
_mabracadabra |
DELETE(4) | 1 |
012345678901
_abracadabra |
_mabrcadabra |
DELETE(5) | 1 |
012345678901
_abracadabra |
_mabradabra |
REPLACE(7, g) | 3 |
012345678901
_abracadabra |
_mabragabra |
7 |
Southeastern European Regional Programming Contest
Bucharest, Romania, October 18, 2003
Problem F
Common Subsequence
Input File: F.IN
Program Source File: F.PAS or F.C or F.CPP or F.JAVA
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Input
abcfbc abfcab
programming contest
abcd
mnp
Output
4
2
0