Advertisement
a53

stiva2

a53
Jan 29th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #define InFile "stiva2.in"
  4. #define OutFile "stiva2.out"
  5. #define NMax 1001
  6. using namespace std;
  7. int n;
  8. char s[NMax+1];
  9. int NrMin[NMax][NMax];
  10.  
  11. int main()
  12. {
  13. ifstream f(InFile);
  14. int i,d,t,j;
  15. f>>s;
  16. n=strlen(s);
  17. f.close();
  18. for(i=0;i<n;++i)
  19. NrMin[i][i]=1;
  20. for(d=2;d<=n;++d)
  21. for(i=0;i+d-1<n;++i)
  22. {
  23. j=i+d-1;
  24. NrMin[i][j]=1+NrMin[i][j-1];
  25. for(t=i;t<j;++t)
  26. if(s[t]==s[j])
  27. if(NrMin[i][t]+NrMin[t+1][j-1]<NrMin[i][j])
  28. NrMin[i][j]=NrMin[i][t]+NrMin[t+1][j-1];
  29. }
  30. ofstream g(OutFile);
  31. g<<2*NrMin[0][n-1]+n<<'\n';
  32. g.close();
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement