Skip navigation

Se dau x si y doua siruri cu m respectiv n numere.

Se cere sa se determine cel mai lung subsir comun celor doua siruri.

Exemplu:

m=8

10 4 20 10 40 2 0 60

n=9

4 90 7 10 70 2 71 81 0

Cel mai lung subsir comun este : 4 10 2 0

Program :

#include
int x[100], y[100], a[50][50], n, m;
void citire (int v[],i nt n)
{
int i;
cin>>n;
for(i=1; i>v[i];

}

int maxim (int x, int y)
{
if(x>y) return x;

else return y;

}

void dinamica()
{
int i, j;

for(i=1; i<=m; i++)
for(j=1 ;j<=n; j++)
if(x[i]==y[j]) a[i][j]=a[i-1][j-1]+1;
else a[i][j]=maxim(a[i-1][j], a[i][j-1]);
cout<<”Lungimea maxima este: ”<<a[m][n]<0 && j>0)

if(x[i]==y[j]){v[++k]=x[i];
i–;j–;}
else { if(a[i][j]==a[i-1][j])i–-;
else j–-;}
for(i=k; i>=1; i–)cout<<v[i]<<” “;
}
int main()
{
citire(x, m);
citire(y, n);
dinamica();
afisare(m, n);
return 0;
}

Lasă un răspuns

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Schimbă )

Twitter picture

You are commenting using your Twitter account. Log Out / Schimbă )

Facebook photo

You are commenting using your Facebook account. Log Out / Schimbă )

Connecting to %s

Follow

Get every new post delivered to your Inbox.