Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <list>
- #include <map>
- #include <set>
- #include <deque>
- #include <stack>
- #include <bitset>
- #include <algorithm>
- #include <functional>
- #include <numeric>
- #include <utility>
- #include <sstream>
- #include <iostream>
- #include <iomanip>
- #include <cstdio>
- #include <cmath>
- #include <cstring>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- string a,b;
- class ReversalChain {
- public:
- int minReversal(string, string);
- };
- int dp[101][101][102][4];
- const int INF=99999999;
- int solve(int L,int R,int start,int direction){
- int d = direction ? -1 : 1;
- bool ok=true;
- for(int i=L;i<R;i++){
- if(b[i] != a[start + (i-L)*d]){
- ok=false;
- break;
- }
- }
- if(ok)return 0;
- if(dp[L][R][start][direction]>=0)return dp[L][R][start][direction];
- int &ret = dp[L][R][start][direction];
- ret=INF;
- for(int l=L;l<R;l++){
- for(int r=R;r>=l;r--){
- if(l==L&&r==R){
- }else{
- if(direction == 0){
- ret = min(ret,
- solve(l,r,start + (r - L) - 1,1) + 1);
- }else{
- ret = min(ret,
- solve(l,r,start - (r - L) + 1, 0) + 1);
- }
- }
- if(b[r-1] != a[start + (r-1-L)*d]){
- break;
- }
- }
- if(b[l] != a[start + (l-L)*d])break;
- }
- cout<<L<<","<<R<<","<<start<<","<<direction<<":"<<ret<<endl;
- return ret;
- }
- int ReversalChain::minReversal(string init, string goal) {
- memset(dp,-1,sizeof(dp));
- a=init;
- b=goal;
- int ret=solve(0,init.size(),0,0);
- memset(dp,-1,sizeof(dp));
- reverse(a.begin(),a.end());
- ret=min(ret,solve(0,init.size(),0,0)+1);
- if(ret>=INF)ret=-1;
- return ret;
- }
Add Comment
Please, Sign In to add comment