Guest User

Untitled

a guest
Dec 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstring>
  18. #include <cstdlib>
  19. #include <ctime>
  20.  
  21. using namespace std;
  22.  
  23. string a,b;
  24.  
  25. class ReversalChain {
  26. public:
  27. int minReversal(string, string);
  28. };
  29.  
  30. int dp[101][101][102][4];
  31. const int INF=99999999;
  32. int solve(int L,int R,int start,int direction){
  33. int d = direction ? -1 : 1;
  34. bool ok=true;
  35. for(int i=L;i<R;i++){
  36. if(b[i] != a[start + (i-L)*d]){
  37. ok=false;
  38. break;
  39. }
  40. }
  41. if(ok)return 0;
  42. if(dp[L][R][start][direction]>=0)return dp[L][R][start][direction];
  43. int &ret = dp[L][R][start][direction];
  44. ret=INF;
  45. for(int l=L;l<R;l++){
  46. for(int r=R;r>=l;r--){
  47. if(l==L&&r==R){
  48.  
  49. }else{
  50. if(direction == 0){
  51. ret = min(ret,
  52. solve(l,r,start + (r - L) - 1,1) + 1);
  53. }else{
  54. ret = min(ret,
  55. solve(l,r,start - (r - L) + 1, 0) + 1);
  56. }
  57. }
  58. if(b[r-1] != a[start + (r-1-L)*d]){
  59. break;
  60. }
  61. }
  62. if(b[l] != a[start + (l-L)*d])break;
  63. }
  64. cout<<L<<","<<R<<","<<start<<","<<direction<<":"<<ret<<endl;
  65. return ret;
  66. }
  67. int ReversalChain::minReversal(string init, string goal) {
  68. memset(dp,-1,sizeof(dp));
  69. a=init;
  70. b=goal;
  71. int ret=solve(0,init.size(),0,0);
  72. memset(dp,-1,sizeof(dp));
  73. reverse(a.begin(),a.end());
  74. ret=min(ret,solve(0,init.size(),0,0)+1);
  75. if(ret>=INF)ret=-1;
  76. return ret;
  77. }
Add Comment
Please, Sign In to add comment