Wed Feb 29, 2012 2:59 pm
Admin - Code:
Cho 2 số a,b . Tìm số c là con của 2 số a,b sao cho số c là lớn nhất có thể .
Đk : 1<=a,b<=100¬100
Dữ liệu vào : - Dòng 1 : Ghi số a.
- Dòng 2 : Ghi số b.
Dữ liệu ra : - Dòng 1 : Ghi độ dài xâu c .
- Dòng 2 : Ghi ra xâu c.
Chương trình :
-----------------------------
- Code:
Program Bai1;
Const fi='bai.inp';
fo='bai.out';
Var max1,n,m:Integer;
l:array[0..100,0..100] Of Integer;
c,a,b:String;
f,g:text;
Procedure doctep;
Begin
Assign(f,fi);
Reset(f);
Readln(f,a);
Readln(f,b);
End;
Procedure khoitao;
Var i,j:Integer;
Begin
Max1:=0;
Assign(g,fo);
Rewrite(g);
Fillchar(l,sizeof(l),0);
End;
Function max(x,y:Integer):Integer;
Begin
If x > y then max:=x Else max:=y;
End;
Procedure tinh;
Var i,j:Integer;
Begin
For i:=length(a) downto 1 do
For j:=length(b) downto 1 do
Begin
If a[i]=b[j] then
l[i,j]:=1+l[i+1,j+1]
Else
l[i,j]:=max(l[i+1,j],l[i,j+1]);
End;
End;
Procedure tinh1;
Begin
End;
Procedure truy;
VAr ch:char;ii,jj,k,i,j:Integer;
Begin
c:='';
max1:=0;
For ch := '9' downto '1' do
Begin
i:=pos(ch,a); j:=pos(ch,b);
IF l [i,j] > max1 then
Begin
max1:=l[i,j];
c:=ch;
End;
End;
i:=pos(c,a)+1;
j:=pos(c,b)+1;
For k:=max1-1 downto 1 do
Begin
For ch:='9' downto '0' do
Begin
ii:=i;jj:=j;
While (ii <=length(a)) and ( a[ii] <> ch) do inc(ii);
While (jj <=length(b)) and ( b[jj] <> ch ) do inc(jj);
IF l[ii,jj]=k then
Begin
c:=c+ch;
i:=ii+1;
j:=jj+1;
Break;
End;
End;
End;
End;
Procedure xuat;
VAr i,j:Integer;
Begin
For i:=1 to length(a) do
Begin
fOr j:=1 to length(b) do
Write(l[i,j]:3);
Writeln;
End;
Writeln(g,length(c):3);
For i:=1 to length(c) do
Writeln(g,c[i]:3);
Close(g);
End;
Begin
doctep;
khoitao;
tinh;
truy;
xuat;
End.