您的位置:首页 > 其它

Shuffle'm Up poj 3087

2015-02-06 23:08 387 查看
/***********************************************
 * Author: fisty
 * Created Time: 2015/2/6 16:43:28
 * File Name   : 1_G.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, c, d) for(int i = c;i < d; i++)
#define MAX_N 1010
string c;
map<string, int> hash;
int vis[MAX_N][MAX_N];
int n,ok, ans, cnt;

void dfs(string a, string b, int res){
    if(!hash.count(a)) hash[a] = cnt++;
    if(!hash.count(b)) hash[b] = cnt++;
    if(vis[hash[a]][hash[b]]) return ;
    vis[hash[a]][hash[b]] = 1;
    string s;
    s.clear();
    FOR(i, 0, n){
        s.push_back(b[i]);
        s.push_back(a[i]);
    }
    if(s == c){
        if(res < ans){
            ans = res;
        }
        ok = 1;
        return ;
    }
    a.clear(); b.clear();
    FOR(i, 0, n){
        a.push_back(s[i]);
    }
    FOR(i, n, n*2){
        b.push_back(s[i]);
    }
    dfs(a, b, res+1);
    return ;
}
int main() {
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    int kcase = 1;
    while(t--){
        cout << kcase++ << " ";
        string a, b;
        cin >> n >> a >> b >> c;
        memset(vis, 0, sizeof(vis));
        ok = 0; ans = INF;
        cnt = 0;
        dfs(a, b, 1);
        if(ok){
            cout << ans << endl;
        }else{
            cout << -1 << endl;
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: