POJ 3087 Shuffle ' m Up (DFS)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.