Dynamic Programming

Could you show me how could I obtain all the longest common subsequences (lcm-s) of two strings?

Ex: a=578459
b=18754229

lcm: 549
859

I already have this code and it only shows me one lcm, but I don't know how to improve it so I could obtain all the lcm-s.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using namespace std;
#include <fstream>
#include <string.h>
ifstream f ("codul.in");
ofstream g ("codul.out");

int main ()
{

    char s1[204], s2[204],d[204][204],sir[204];
    int x,y,i,j,bst=0;

    f.getline (s1,204);
    f.getline (s2,204);

    x=strlen(s1);
    y=strlen(s2);

    for (i=1; i<=x; i++)
    {
        for (j=1; j<=y; j++)
        {
            if (s1[i-1]==s2[j-1])
            {
                d[i][j]=d[i-1][j-1]+1;
            }
            else
            {
                if (d[i-1][j]<d[i][j-1]) d[i][j]=d[i][j-1];
                else d[i][j]=d[i-1][j];
            }
        }
    }
    for (i=x, j=y; i;)
    {
        if (s1[i-1]==s2[j-1])
        {
            sir[++bst]=s1[i-1];
            i--;
            j--;
        }
        else if (d[i-1][j]<d[i][j-1]) j--;
        else i--;
    }




    for (i=bst; i>=1; i--) g<<sir[i];
    f.close ();
    g.close ();
    return 0;
}


Topic archived. No new replies allowed.