The title describes the most of your played card games (and if you haven ' t, why not???) in which the deck of cards are randomized by shuf?i ng it one or more times.
A perfect Shuf?e is a type of shuf?e where the initial deck was divided exactly in half, and the both halves are perfectly I Nterleaved. For example, a deck consisting of eight cards abcdefgh (where A is the top card of the deck) would was divided into the HAL Ves ABCD and EFGH and then
Interleaved to get AEBFCGDH. Note the This shuf?e the original top card (A) stays on top-this type of perfect shuf?e are called an out-shuf?e. An equally valid perfect shuf?e would start with the. RST card from the second half and result in Eafbgchd-this are known As an in-shuf?e.
While normal shuf?ing does a good job @ randomizing a deck, perfect shuf?es result in only a small number of possible Ord Erings. For example, if we perform multiple out-shuf?es on the deck above, we obtain the following:
abcdefgh→aebfcgdh→acegbdfh→abcdefgh→
So after 3 out-shuf?es, the deck are returned to their original state. A Similar thing happens if we perform multiple in-shuf?es on the 8-card deck, though in the case it would take 6 shuf?es b Efore we get back to where we started. With a standard deck card, only 8 out-shuf?es was needed before the deck is returned to its original order (talented Ma Gicians can make use of this result in many of their tricks). These shuf?es can also is used on decks with an odd number of cards, but we had to be a little careful:for out-shuf?es, The. RST half of the deck must has 1 more card than the
Second half; For In-shuf?es, it ' s the exact opposite. For example, a out-shuf?e on the deck ABCDE results in Adbec, while a in-shuf?e results in cadbe.
For this problem you'll be given the size of a deck and must determine how many in-or out-shuf?es it takes to return th e deck to its pre-shuf?ed order. Enter the input consists of one line containing a positive integer n≤1000 (the size of the D ECK) followed by either the word in or out, indicating whether you should perform in-shuf?es or out-shuf?es. Output for each TES T case, output the case number followed by the number of in-or out-shuf?es required to return the deck to its original or Der. Sample Input
8 out
Sample output
3
Tips for solving problems:
Test Instructions: There are n cards, let you shuffle, the most perfect one is alternating insertion, example: There are abcdefgh eight (even cards), divided into abcd,efgh two groups, according to "out-shuffle" after becoming AEBFCGDH, according to "into-shuffle" And then the EAFBGCHD. Odd cards: ABCDE, according to "out-shuffle" after becoming Adbec, according to "into-shuffle" after is cadbe. The output is shuffled several times and then becomes the initial sequence.
Do before you need to judge is the shuffle, or into the shuffle, and then determine whether there are odd cards or even cards. As long as one of the cases is written, the rest of the adjustment is OK.
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intMain ()8 {9 stringA="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrst Uvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmno Pqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijkl Mnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefg Hijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcd Efghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxy Zabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrst Uvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxabcdefghijklmnopq RstuvwxyzabcdefghijklmnopqrstuvwX00000 ";Ten //The title of n is less than 1000, the previous line is to obtain 1000 characters; One intN; ACin>>N; - stringT=A.SUBSTR (1, n); - ints=t.length (); the strings1,s2; - strings3=T; - stringinput; - intoutput=0; +Cin>>input; - if(input==" out"){ + if(s%2==0){ A while(1){ atS1=S3.SUBSTR (0, s/2); -S2=s3.substr (s/2, s/2); - //cout<<s1<<s2; - inti1=0; - for(intI=0; i<s/2; i++){ -s3[i1++]=S1[i]; ins3[i1++]=S2[i]; - } tooutput++; + //cout<<output; - //cout<<s3; the if(s3==t) { *printf"%d", output); $output=0;Panax Notoginseng Break; - } the } +}Else{ A while(1){ theS1=S3.SUBSTR (0, s/2+1); +S2=s3.substr (s/2+1, s/2); - //cout<<s1<<s2; $ inti1=0; $ for(intI=0; i<s/2; i++){ -s3[i1++]=S1[i]; -s3[i1++]=S2[i]; the } -s3[i1]=s1[s/2];Wuyioutput++; the //cout<<output; - //cout<<s3; Wu if(s3==t) { -printf"%d", output); Aboutoutput=0; $ Break; - } - } - } A } + if(input=="inch"){ the if(s%2==0){ - while(1){ $S1=S3.SUBSTR (0, s/2); theS2=s3.substr (s/2, s/2); the //cout<<s1<<s2; the inti1=0; the for(intI=0; i<s/2; i++){ -s3[i1++]=S2[i]; ins3[i1++]=S1[i]; the } theoutput++; About //cout<<output; the //cout<<s3; the if(s3==t) { theprintf"%d", output); +output=0; - Break; the }Bayi } the}Else{ the while(1){ -S1=S3.SUBSTR (0, s/2); -S2=s3.substr (s/2, s/2+1); the //cout<<s1<<s2; the inti1=0; the for(intI=0; i<s/2; i++){ thes3[i1++]=S2[i]; -s3[i1++]=S1[i]; the } thes3[i1]=s2[s/2]; theoutput++;94 //cout<<output; the //cout<<s3; the if(s3==t) { theprintf"%d", output);98output=0; About Break; - }101 }102 }103 }104 the return 0;106}
2106 problem F shuf?ing along