Title Link: Shuffle ' m up
Test instructions: There are two character sequences with a and b length n, and now define the operation:
The characters A, B are merged into a sequence C, and the top N of c is classified as a, and the bottom n is a B
Given a, b and a target sequence C, ask at least how many operations a, C to convert
Parse: Put a, b into the hash table, and then simulate the operation process directly DFS can.
AC Code:
#include <cstdio> #include <iostream> #include <cstring> #include <map>using namespace Std;int n , ans, cnt;string c;map<string, int> sti;bool vis[5010][5010], tg;void dfs (string A, string b, int dep) {if (!sti. Count (a)) sti[a] = cnt++; Use map instead of hash table if (!sti.count (b)) sti[b] = cnt++; if (Vis[sti[a]][sti[b]]) return; VIS[STI[A]][STI[B]] = true; string S; for (int i=0; i<n; i++) {//Cross S.push_back (B[i]); S.push_back (A[i]); } if (s = = c) {if (dep < ans) ans = dep; TG = true; return; } a.clear (); B.clear (); for (int i=0; i<n; i++) A.push_back (S[i]); The top n is classified as a for (int i=n; i<2*n; i++) B.push_back (S[i]); The following n are classified as Dfs B (A, b, dep+1);} int main () {//Freopen ("In.txt", "R", stdin); int t; scanf ("%d", &t); for (int kase=1; kase<=t; kase++) {string A, B; CNT = 0; ans = 0x7ffffff; cin>>n>>a>>b>>c; Memset (vis, false, sizeof (VIS)); TG = false; Sti.clear (); DFS (A, B, 1); printf ("%d", Kase); if (TG) printf ("%d\n", ans); Else puts ("-1"); } return 0;}
Copyright NOTICE: This article is sxk original article, reprint please attach this article link ^_^
POJ 3087 Shuffle ' m Up (DFS)