FBZ串问题。已知一个由0,1字符组成的长度为2n的字符串。请按以下规则将已给出的字符串分解为FBZ串:
(1)若其中字符全为'1',则称其为'B'串;
(2)若其中字符全为'0',则称其为'Z'串;
(3)若不全为'0',同时也不全为'1',则称'F'串。若此串为F串,则应将此串分解为2个长为2n-1的子串。
对分解后的子串,仍按以上规则继续分解,直到全部为B串或为Z串为止。
例如n=3时,给出0-1串为:'10111001'
最后输出:FFFBZBFFBZFZB
问题:给出01串,分解成FBZ串。
程序如下:
Program EXP-5;
CONST N = 8;
VAR
I,J,ST11,ST12,ST2,S,T : INTEGER;
STR1 : ARRAY[1..N*2, 1..N] OF CHAR;
STR2 : ARRAY[1..40] OF CHAR;
BEGIN
FOR I := 1 TO N*2 DO
FOR J := 1 TO N DO STR1[I,J] := '□';
ST11 := 1; ST12 := 1; ST2 := 0;
FOR I := 1 TO N DO READ(STR1[1,I]); READLN; 4%
WHILE st12<=st11 DO
BEGIN
S := 0; T := 0;
FOR I := 1 TO N DO
BEGIN
IF STR1[ST12,I] = '1' THEN S := S + 1;
IF STR1[ST12,I] = '0' THEN T := T + 1
END;
IF t=0 THEN BEGIN 2%
ST2 := ST2 + 1; STR2[ST2] := 'B'
END
ELSE IF s=0 THEN BEGIN 2%
ST2 := ST2+1; STR2[ST2]:='Z'
END
ELSE BEGIN
ST2 := ST2+1; STR2[ST2] := 'F'; J := (S+T) DIV 2;
FOR S := N*2-2 DOWNTO st12+1 DO 3%
FOR T := 1 TO N DO
STR1[S+2,T] := STR1[S,T];
ST11 := ST11 + 2;
FOR I := 1 TO J DO
BEGIN
STR1[ST12+1,I] := STR1[ST12,I];
STR1[ST12+2,I] := st1[st12,i+j] 4%
END;
FOR I := j+1 to n DO BEGIN 2%
STR1[ST12+1,I] := '□'; STR1[ST12+2,I] := '□'
END
END
ST12:=ST12+1
END;
FOR I := 1 TO ST2 DO WRITE(STR2[I]); WRITELN
END.