fork download
  1. const maxn = 255;
  2. var i,j,k,n:byte;
  3. s:string;
  4. f: array[0..maxn+1,0..maxn+1] of boolean;
  5. dp: array[0..maxn] of byte;
  6. begin
  7. readln(s);
  8. n:=length(s);
  9. for i:=0 to n do
  10. for j:=0 to n do
  11. f[i,j]:=false;
  12. for i:=0 to n do
  13. f[i,i]:=true;
  14. for k:=1 to n-1 do
  15. for i:=1 to n-k do
  16. begin
  17. j:=i+k;
  18. f[i,j]:=(f[i+1,j-1] and (s[i]=s[j])) or ((j-i=1) and (s[i]=s[j]));
  19. end;
  20. fillchar(dp,sizeof(dp),maxn);
  21. dp[0]:=0;
  22. for i:=1 to n do
  23. begin
  24. for j:=1 to i do
  25. if f[j,i] and (dp[j-1]+1<dp[i]) then dp[i]:=dp[j-1]+1;
  26. end;
  27. writeln(dp[n]);
  28. end.
Success #stdin #stdout 0s 340KB
stdin
bobseesanna
stdout
3