搜索与回溯 字符序列

SSL 1893
题目大意
给出n,求出有多少长度为n由‘A’‘B’‘c’组成的字符序列。
要求:相邻子序列不能重合。
例子:
ABCAB是合法的,AB没有重合。
ABABC和BABAC不是合法的。

const
  b:array[1..3]of char=('A','B','C');
var
  n,s:longint;
  a:array[1..10]of char;
procedure init;
begin
  readln(n);
end;

procedure print;
var
  i:longint;
begin
  for i:=1 to n do write(a[i]);
  writeln;
end;

function check(k:longint):boolean;
var
  i,j,l:longint;
begin
  check:=false;
  for i:=1 to k div 2 do
   begin
     j:=0;
     for l:=1 to i do
      if (a[k-l+1]<>a[k-i-l+1]) then
       inc(j);
     if j=0 then exit(true);
   end;
end;

procedure search(k:longint);
var
  i,j:longint;
begin
  if k>n then begin print;inc(s); end
   else
  for i:=1 to 3 do
   begin
     a[k]:=b[i];
     if k>1 then if check(k) then continue;
     search(k+1);
     a[k]:=' ';
   end;
end;

begin
  init;
  search(1);
  writeln(s);
end.
@Qyh
冒牌标记,谨防某些人copy

搜索解。重点在check那个自定义过程里。
扫描有没有相邻且重叠的序列。
很难,主要是check那里的条件很难设置。

你可能感兴趣的:(搜索与回溯 字符序列)