求原字串与其反字串的最长公共子序列,最后结果用n减去LCS的长度即可

program poj1159;
var
  s1,s2:ansistring;
  n,i,j:integer;
  f:array[0..1,0..5001] of integer;
function max(p,q:integer):integer;
begin
  if p>q then max:=p
    else max:=q;
end;
begin
  readln(n);
  readln(s1);
  fillchar(f,sizeof(f),0);
  s2:='';
  for i:=n downto 1 do
    s2:=s2+s1[i];
  for i:=1 to n do
  begin
    for j:=1 to n do
      if s1[i]=s2[j] then f[1,j]:=f[0,j-1]+1
        else f[1,j]:=max(f[0,j],f[1,j-1]);
    f[0]:=f[1];
  end;
  writeln(n-f[1,n]);
end.