很容易脑补出来的区间dp O(n3)的

var f:array[0..51,0..51] of longint;
    i,n,j,l,k:longint;
    s:string;

function min(a,b:longint):longint;
  begin
    if a>b then exit(b) else exit(a);
  end;

begin
  readln(s);
  n:=length(s);
  for i:=1 to n do
    f[i,i]:=1;

  for l:=2 to n do
    for i:=1 to n-l+1 do
    begin
      j:=i+l-1;
      f[i,j]:=l;
      if s[i]=s[j] then
      begin
        f[i,j]:=min(f[i+1,j],f[i,j-1]);
        f[i,j]:=min(f[i,j],f[i+1,j-1]+1);
      end;
      for k:=i to j-1 do
        f[i,j]:=min(f[i,j],f[i,k]+f[k+1,j]);
    end;
  writeln(f[1,n]);
end.

 

posted on 2014-10-26 16:31  acphile  阅读(112)  评论(0编辑  收藏  举报