Simple POJ 3087 Shuffle ' m up, enumeration.

Source: Internet
Author: User
Tags shuffle

Description

A common pastime for poker players at a poker table was to shuffle stacks of chips. Shuffling chips is performed by starting with the stacks of poker chips, S1 and S2, each stack containin G C chips. Each of the stacks may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown being Low for C = 5:

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of this chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, fo Llowed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 are placed on top O F S12.

After the shuffle operation, S12 are split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new s2. The shuffle operation is repeated to form a new S12.

For this problem, you'll write a program to determine if a particular resultant stack S12 can be formed by Shuf Fling stacks some number of times.

The title is shuffle, according to his rules to wash, this process can be directly simulated, you can find the number is less than or equal to C (from the bottom to the number. Multiplied by 2, greater than C is multiplied by 2 then-1, and then take the mold, so that you can see the shuffle will be circulating, because only 2 C type number, for some, the first 2c+1 times must have been before the number, each will cycle, then this whole must also cycle it, I feel is the cycle of 2C.

Then the shuffle is good, there is a cycle of words even if it is impossible.

The code is as follows:

#include <iostream>#include<cstring>using namespacestd;Charend[ About];Charsta[ About];intsnum[ About];intC;voidChange () { for(intI=1; i<=2*c;++i)if(snum[i]<=C) Snum[i]*=2; ElseSnum[i]= (snum[i]-c) *2-1;}BOOLjudge () { for(intI=1; i<=c*2;++i)if(end[snum[i]-1]!=sta[i-1])            return 0; return 1;}voidSlove () { for(intI=1; i<=2*c;++i) snum[i]=i; intcn1=1;    Change ();  while(snum[1]!=1)    {        if(judge ()) {cout<<ans<<Endl; return;        } change (); ++ans; }    if(judge ()) cout<<ans<<Endl; Elsecout<<-1<<Endl;}intMain () {Ios::sync_with_stdio (false); intT; Chartemp[ the]; CIN>>u;  for(intcas=1; cas<=t;++CAs) {cin>>b; CIN>>Sta; CIN>>temp;        strcat (sta,temp); CIN>>End; cout<<cas<<' ';    Slove (); }    return 0;}
View Code

Simple POJ 3087 Shuffle ' m up, enumeration.

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.