Cod sursa(job #1626960)

Utilizator Antonio9227wdasdas Antonio9227 Data 3 martie 2016 13:10:48
Problema Cel mai lung subsir comun Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <stdio.h>
#include <stack>

using namespace std;
short m,n;
short table[1025][1025];
short action[1025][1025];
unsigned char v1[1025],v2[1025];
stack<unsigned char> sir;

int main()
{
    freopen("cmlsc.in","r",stdin);
     freopen("cmlsc.out","w",stdout);
    scanf("%d%d",&m,&n);

    for(int i=0; i<m; i++)
        scanf("%d",&v1[i]);

    for(int i=0; i<n; i++)
        scanf("%d",&v2[i]);

    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
        {
            if(v1[i-1]==v2[j-1])
            {
                table[i][j]=table[i-1][j-1]+1;
                //0=nothing;1=left;2=up;3=diag;
                action[i][j]=3;
            }
            else
            {
                if(table[i-1][j]>table[i][j-1])
                {
                    table[i][j]=table[i-1][j];
                    action[i][j]=2;
                }
                else
                {
                    table[i][j]=table[i][j-1];
                    action[i][j]=1;
                }
            }
        }

    printf("%d\n",table[m][n]);


    int x=m,y=n;

    while(table[x][y])
    {
        switch(action[x][y])
        {
        case 3:
            sir.push(v2[y-1]);
            x--;
            y--;
            break;
        case 1:
            y--;
            break;
        case 2:
            x--;
            break;
        }
    }

    while(!sir.empty()) {
        printf("%d ",sir.top());
        sir.pop();
    }

}