You are on page 1of 6

levenshtein

#include <fstream>
#include <string.h>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
char a[100],b[100],sol[100][100],op;
int m,n,i,j,k,c[100][100],mini,t;
void afis(int &i, int &j, char &op)
{
int l=0,col=0;
mini=1000;
if (i>0 && j>0 && mini>=c[i-1][j-1])
{
mini=c[i-1][j-1];
l=i-1; col=j-1;
op='m';
}
if (i>0 && c[i-1][j]<mini)
{
mini=c[i-1][j];
l=i-1; col=j;
op='s';
}
if (j>0 && c[i][j-1]<mini)
{
mini=c[i][j-1];
l=i; col=j-1;
op='a';
}
if (c[i][j]==mini)
op='v';
i=l;
j=col;
}
int main()
{
f.getline(a+1,100);
//f.get();
f.getline(b+1,100);
m=strlen(a+1);
n=strlen(b+1);
for (i=0;i<=m;i++)
c[i][0]=i;
for (j=0;j<=n;j++)
c[0][j]=j;
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
if (a[i]==b[j])
c[i][j]=c[i-1][j-1];
else
{
mini=c[i-1][j-1];
if (c[i-1][j]<mini)
mini=c[i-1][j];

if (c[i][j-1]<mini)
mini=c[i][j-1];
c[i][j]=mini+1;
}
g<<c[m][n]<<endl;
i=m; j=n;
while (i+j>0)
{
afis(i,j,op);
if (op!='v')
{
int x=0;
k++;
for (t=1;t<=j;t++)
sol[k][x++]=b[t];
for (t=i+1;t<=m;t++)
sol[k][x++]=a[t];
sol[k][x++]='-';
sol[k][x++]=op;
sol[k][x++]='-';
}
}
for (k=c[m][n];k>=0;k--)
g<<sol[k];
g<<b+1;
}

//Suma maxima in dreptunghi


4 6
7 2 4 5 9 2
1 6 7 3 2 4
9 2 5 7 6 5
3 1 2 4 7 5
# include <fstream>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<ctime>
#include<cctype>
using namespace std;
int i,j,k,maxi,a[50][50],s[50][50],p[50][50],n,m;
int main()
{ ifstream f("date.in");
ofstream g("date.out");
f>>n>>m;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
f>>a[i][j];
for (i=1;i<=m;i++)
s[n][i]=a[n][i];
for(i=n-1;i>=1;i--)
for (j=1;j<=m;j++)
if (j==1)
if (s[i+1][j]>=s[i+1][j+1])
{s[i][j]=s[i+1][j]+a[i][j]; p[i][j]=2;}

else {s[i][j]=s[i+1][j+1]+a[i][j]; p[i][j]=3;}


else if (j==m)
if (s[i+1][j]>=s[i+1][j-1])
{s[i][j]=s[i+1][j]+a[i][j]; p[i][j]=2;}
else {s[i][j]=s[i+1][j-1]+a[i][j]; p[i][j]=1;}
else
if (s[i+1][j]>=s[i+1][j+1] && s[i+1][j]>=s[i+1][j-1])
{s[i][j]=s[i+1][j]+a[i][j]; p[i][j]=2;}
else if (s[i+1][j+1]>=s[i+1][j] && s[i+1][j+1]>=s[i+1][j-1])
{s[i][j]=s[i+1][j+1]+a[i][j]; p[i][j]=3;}
else if (s[i+1][j-1]>=s[i+1][j] && s[i+1][j-1]>=s[i+1][j+1])
{s[i][j]=s[i+1][j-1]+a[i][j]; p[i][j]=1;}
for (i=1; i<=m;i++)
if (s[1][i]>maxi)
{maxi=s[1][i];
k=i;}
g<<"Suma maxima este: "<<maxi<<'\n';
g<<a[1][k]<<" ";
j=k;
for (i=1;i<n;i++)
if (p[i][j]==2)
g<<a[i+1][j]<<" ";
else if (p[i][j]==1)
{g<<a[i+1][j-1]<<" "; j--;}
else
{g<<a[i+1][j+1]<<" "; j++;}
return 0;
}

//matriceh
# include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int a[100][100];
void afisare(int l, int c)
{int k;
if(c-l>=1)
{
k=a[c][l];
if(k!=l)
afisare(l,k);
if(k+1!=c)
afisare(k+1,c);
cout<<l<<" "<<c<<endl;
}
}
int main()
{
int k,i,j,n,v[100],len,mini;
f>>n;
for(i=1;i<=n+1;i++)
f>>v[i];
for(len=1;len<n;len++)
for(i=1;i<=n-len;i++)

{
j=i+len;
a[i][j]=32123;
for(k=i;k<j;k++)
{
mini=a[i][k]+a[k+1][j]+v[i]*v[k+1]*v[j+1];
if(mini<a[i][j])
{
a[i][j]=mini;
a[j][i]=k;
}
}
}
cout<<a[1][n]<<endl;
afisare(1,n);
return 0;
}

//teava
# include <iostream>
#include <fstream>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int s,n,v[100],r[100],p[100];
int main()
{
f>>s;
f>>n;
int i,j;
for(i=1;i<=n;i++)
f>>v[i];
for(i=1;i<=n;i++)
{
if(!r[v[i]])
r[v[i]]=i;
for(j=1;j<=s-v[i];j++)
if(r[j]&&r[j]!=i&&(!r[j+v[i]]))
r[j+v[i]]=i;
}
if(r[s])
for(i=s;r[i]>0;i=i-v[r[i]])
cout<<v[r[i]]<<" ";
return 0;
}

/ subsir crescator maxim


# include <fstream>
#include<iostream>
using namespace std;
int v[10000],n,i,L[1000],maxx,mx,k,t;
int main()
{
ifstream f("subsir.in");
f>>n;
for(i=1;i<=n;i++)
f>>v[i];
L[n]=1;
for(k=n-1;k>0;k--)
{
mx=0;
for(i=k+1;i<=n;i++)
if(v[i]>=v[k] && L[i]>mx)
mx=L[i];
L[k]=mx+1;
if(L[k]>maxx)
{
maxx=L[k];
t=k;
}
}
cout<<"lungimea maxima:"<<maxx;
cout<<endl<<v[t]<<' ';
for(i=t+1;i<=n;i++)
if ((v[i]>=v[t]) && (L[i]==maxx-1))
{
cout<<v[i]<<' ';
maxx--;
}
return 0;
}

//subsir comun maximal


# include <fstream>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[1024],b[1024],c[1024][1024],i,j,sub[1024],lmax;
ifstream f("date.in");
int main(){
f>>n>>m;
for(i=1;i<=n;i++)f>>a[i];
for(i=1;i<=m;i++)f>>b[i];
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(a[i]==b[j])c[i][j]=c[i-1][j-1]+1;
else c[i][j]=max(c[i-1][j],c[i][j-1]);
lmax=c[n][m];

i=n;j=m;
while(c[i][j])
if(a[i]==b[j]){sub[lmax--]=a[i];i--;j--;}
else if(c[i][j]==c[i][j-1])j--;
else i--;
cout<<c[n][m];
cout<<endl;
for(i=1;i<=c[n][m];i++) cout<<sub[i]<<" ";
return 0;
}

//si ultima cred, rucscacul discret


# include <iostream>
#include <fstream>
using namespace std;
struct da{
int g,p;}a[100];
int s[100][100],poz[100][100], G, n, i, j;
ifstream f("date.in");
int main()
{
f>>n>>G;
int i,j,k;
for(i=1;i<=n;i++)
f>>a[i].g>>a[i].p;
for(i=1;i<=n;i++)
{ for(j=1;j<=G;j++)
if(a[i].g<=j && a[i].p+s[i-1][j-a[i].g]>s[i-1][j])
{
s[i][j]=s[i-1][j-a[i].g]+a[i].p;
poz[i][j]=i;
}
else {s[i][j]=s[i-1][j];
poz[i][j]=poz[i-1][j];
}
}
cout<<s[n][G]<<endl;
i=n; j=G;
while(poz[i][j]!=0)
{
k=poz[i][j];
cout<<k<<" "<<a[k].p<<endl;
j=j-a[k].g;
while(poz[i][j]==k)i--;
}
return 0;
}