Most of you have played card games (and if you haven’t, why not???) in which the deck of cards is randomized by shuf?ing it one or more times.
A
perfect shuf?e is a type of shuf?e where the initial deck is divided exactly in
half, and the two halves are perfectly interleaved. For example, a deck
consisting of eight cards ABCDEFGH (where A is the top card of the deck) would
be divided into two halves ABCD and EFGH and then
interleaved to get AEBFCGDH. Note that in this shuf?e
the original top card (A) stays on top —this type of perfect shuf?e is 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 is known as an
in-shuf?e.
While normal shuf?ing does a
good job at randomizing a deck, perfect shuf?es result in only a small number of
possible orderings. 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 is returned to its
original state. A similar thing happens if we perform multiple in-shuf?es on an
8-card deck, though in this case it would take 6 shuf?es before we get back to
where we started. With a standard 52 card deck, only 8 out-shuf?es are needed
before the deck is returned to its original order (talented magicians can make
use of this result in many of their tricks). These shuf?es can also be used on
decks with an odd number of cards, but we have to be a little careful: for
out-shuf?es, the ?rst half of the deck must have 1 more card than
the
second half; for in-shuf?es, it’s
the exact opposite. For example, an out-shuf?e on the deck ABCDE results in
ADBEC, while an in-shuf?e results in CADBE.
For this problem you will be given the size of a deck
and must determine how many in- or out-shuf?es it takes to return the deck to
its pre-shuf?ed order.
The input consists of one line
containing a positive integer n ≤ 1000 (the size of the deck) followed by either
the word in or out, indicating whether you should perform in-shuf?es or
out-shuf?es.
For each test case, output the
case number followed by the number of in- or out-shuf?es required to return the
deck to its original order.
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4
5 using namespace std;
6
7 int main()
8 {
9 string a="0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX00000";
10 //题目中n小于1000,上一行是为了获得1000个字符;
11 int n;
12 cin>>n;
13 string t=a.substr(1,n);
14 int s=t.length();
15 string s1,s2;
16 string s3=t;
17 string input;
18 int output=0;
19 cin>>input;
20 if(input=="out"){
21 if(s%2==0){
22 while(1){
23 s1=s3.substr(0,s/2);
24 s2=s3.substr(s/2,s/2);
25 //cout<<s1<<s2;
26 int i1=0;
27 for(int i=0;i<s/2;i++){
28 s3[i1++]=s1[i];
29 s3[i1++]=s2[i];
30 }
31 output++;
32 //cout<<output;
33 //cout<<s3;
34 if(s3==t){
35 printf("%d",output);
36 output=0;
37 break;
38 }
39 }
40 }else{
41 while(1){
42 s1=s3.substr(0,s/2+1);
43 s2=s3.substr(s/2+1,s/2);
44 //cout<<s1<<s2;
45 int i1=0;
46 for(int i=0;i<s/2;i++){
47 s3[i1++]=s1[i];
48 s3[i1++]=s2[i];
49 }
50 s3[i1]=s1[s/2];
51 output++;
52 //cout<<output;
53 //cout<<s3;
54 if(s3==t){
55 printf("%d",output);
56 output=0;
57 break;
58 }
59 }
60 }
61 }
62 if(input=="in"){
63 if(s%2==0){
64 while(1){
65 s1=s3.substr(0,s/2);
66 s2=s3.substr(s/2,s/2);
67 //cout<<s1<<s2;
68 int i1=0;
69 for(int i=0;i<s/2;i++){
70 s3[i1++]=s2[i];
71 s3[i1++]=s1[i];
72 }
73 output++;
74 //cout<<output;
75 //cout<<s3;
76 if(s3==t){
77 printf("%d",output);
78 output=0;
79 break;
80 }
81 }
82 }else{
83 while(1){
84 s1=s3.substr(0,s/2);
85 s2=s3.substr(s/2,s/2+1);
86 //cout<<s1<<s2;
87 int i1=0;
88 for(int i=0;i<s/2;i++){
89 s3[i1++]=s2[i];
90 s3[i1++]=s1[i];
91 }
92 s3[i1]=s2[s/2];
93 output++;
94 //cout<<output;
95 //cout<<s3;
96 if(s3==t){
97 printf("%d",output);
98 output=0;
99 break;
100 }
101 }
102 }
103 }
104
105 return 0;
106 }