Bài toán Nén xâu

2,559 views
Skip to first unread message

tienph...@gmail.com

unread,
Feb 19, 2013, 8:24:20 PM2/19/13
to thcsthait...@googlegroups.com

Bài 1:                     Nén xâu

Một xâu kí tự có thể nén lại thành 1 một xâu mới bằng cách nén các kí tự giống nhau đứng cạnh nhau. Ví dụ trong xâu AAAA sẽ nén thành 4A. Hãy lập trình để nén 1 xâu kí tự in hoa theo cách trên.

Dữ liệu: vào từ file văn bản NENXAU.INP một xâu, mà các kí tự là chữ cái in hoa.

Kết quả: ghi vào file văn bản NENXAN.OUT một xâu kí tự sau khi nén.

VD:  

Nenxau.inp

Nenxau.out

NNNAAAAFGGH

3N4AF2GH

tienph...@gmail.com

unread,
Feb 19, 2013, 8:25:18 PM2/19/13
to thcsthait...@googlegroups.com, tienph...@gmail.com
var
        s:string;
        f:text;
procedure input(var s:string);
        var
                f:text;
        begin
                assign(f,'nenxau.inp');
                reset(f);
                read(f,s);
                close(f);
        end;
procedure nenxau(var s:string);
        var
                snen,stam:string;
                i,j:integer;
        begin
                snen:='';
                stam:='';
                i:=1;
                j:=1;
                repeat
                        while s[j]=s[i] do
                                begin
                                        stam:=stam+s[j];
                                        j:=j+1;
                                end;
                        if length(stam)>1 then
                                begin
                                        str(length(stam),stam);
                                        snen:=snen+stam+s[i];
                                end
                        else
                                snen:=snen+s[i];
                        i:=j;
                        stam:='';
                until i>length(s);
                s:=snen;

        end;

{CHUONG TRINH CHINH}
BEGIN
        input(s);
        nenxau(s);

        {Ghi xau ket qua vao fiel nenxau.out}
        assign(f,'nenxau.out');
        rewrite(f);
        write(f,s);
        close(f);
END.
Reply all
Reply to author
Forward
0 new messages