题意:给出两个字符串,d2字符串在下,d1字符串在上,交叉组成一个字符串,判断初始串进行若干次操作后是否能到目标串
链接:http://poj.org/problem?id=3087
思路:模拟即可
注意点:需要记录该位置是否到达过,用map作为hash即可
以下为AC代码:
Run ID | User | Problem | Result | Memory | Time | Language | Code Length | Submit Time |
13917619 | luminous11 | 3087 | Accepted | 772K | 0MS | G++ | 1802B | 2015-02-27 22:50:10 |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
//const double pi = acos(-1);
//const double eps = 1e-10;
//const int dir[[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
map< string, int > vis;
int m;
string a, b;
string c;
void init()
{
vis.clear();
cin >> m >> a >> b >> c;
}
int solve()
{
queue<string> q;
string tmp;
tmp.resize( 2 * m );
for ( int i = 0; i < m; i ++ ){
tmp[2*i] = b[i];
tmp[2*i+1] = a[i];
}
q.push ( tmp );
vis[tmp] = 1;
while ( ! q.empty() ){
tmp = q.front();
q.pop();
string x ( tmp, 0, m );
string y ( tmp, m, m );
string k;
k.resize( 2 * m );
for ( int i = 0; i < m; i ++ ){
k[2*i] = y[i];
k[2*i+1] = x[i];
}
if ( vis[k] )
return -1;
if ( vis[k] == 0 ){
vis[k] = vis[tmp] + 1;
q.push ( k );
}
if ( k == c ){
return vis[k];
}
}
}
int main()
{
ios::sync_with_stdio( false );
int t;
cin >> t;
for ( int i = 1; i <= t; i ++ ){
init();
cout << i << ' ' << solve() << endl;
}
return 0;
}