## Monday, March 14, 2011

### UVa_10205_Stack_\'em_Up.cpp

poj2469, uva10205,

## Problem E: Stack 'em Up

A standard playing card deck contains 52 cards, 13 values in each of four suits. The values are named 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace. The suits are named Clubs, Diamonds, Hearts, Spades. A particular card in the deck can be uniquely identified by its value and suit, typically denoted of . For example, "9 of Hearts" or "King of Spades". Traditionally a new deck is ordered first alphabetically by suit, then by value in the order given above. The Big City has many Casinos. In one such casino the dealer is a bit crooked. She has perfected several shuffles; each shuffle rearranges the cards in exactly the same way whenever it is used. A very simple example is the "bottom card" shuffle which removes the bottom card and places it at the top. By using various combinations of these known shuffles, the crooked dealer can arrange to stack the cards in just about any particular order.
You have been retained by the security manager to track this dealer. You are given a list of all the shuffles performed by the dealer, along with visual cues that allow you to determine which shuffle she uses at any particular time. Your job is to predict the order of the cards after a sequence of shuffles.

### Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
Input consists of an integer n <= 100, the number of shuffles that the dealer knows. 52n integers follow. Each consecutive 52 integers will comprise all the integers from 1 to 52 in some order. Within each set of 52 integers, i in position j means that the shuffle moves the ith card in the deck to position j.
Several lines follow; each containing an integer k between 1 and n indicating that you have observed the dealer applying the kth shuffle given in the input.

### Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Assume the dealer starts with a new deck ordered as described above. After all the shuffles had been performed, give the names of the cards in the deck, in the new order.

### Sample Input

```1

2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2```

### Output for Sample Input

```King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
3 of Clubs```

## Solution:

ad-hoc, it's like translation with letters.

## Source Code:

Since the main code parts are very similar, here I just paste the code for UVa.
//Mon Mar 14 16:17:01 CDT 2011
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

void initialize(int &N, vector<vector<int> > &v, vector<int> &src)
{
cin >> N;
for (int i = 0; i < 52; i++)
src.push_back(i + 1);
for (int i = 0; i < N; i++)
{
vector<int> temp;
for (int j = 0, tmp; j < 52; j++)
{
cin >> tmp;
temp.push_back(tmp);
}
v.push_back(temp);
}

}

string matchValue(int value)
{
switch (value)
{
case 1:
return "2";
case 2:
return "3";
case 3:
return "4";
case 4:
return "5";
case 5:
return "6";
case 6:
return "7";
case 7:
return "8";
case 8:
return "9";
case 9:
return "10";
case 10:
return "Jack";
case 11:
return "Queen";
case 12:
return "King";
case 0:
return "Ace";
}
return "";
}

string matchSuit(int number)
{
switch (number)
{
case 0:
return "Clubs";
case 1:
return "Diamonds";
case 2:
return "Hearts";
case 3:
}
return "";
}

string translate(int number)
{
int suit = (number - 1) / 13;
int value = number % 13;
return matchValue(value) + " of " + matchSuit(suit);
}

vector<int> oneShuffle(vector<int> src, vector<int> crypt)
{
vector<int> tmp(52, 0);
for (int i = 0; i < src.size(); i++)
{
tmp[i] = src[crypt[i] - 1];
}
return tmp;
}

void solve(vector<int> &src, vector<vector<int> > shuffle)
{
for (int i = 0; i < shuffle.size(); i++)
{
int value;
cin >> value;
src = oneShuffle(src, shuffle[value - 1]);
}
}

void print(vector<int> src)
{
for (int i = 0; i < 52; i++)
cout << translate(src[i]) << endl;
}

int main(int argc, char* argv[])
{
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int T;
cin >> T;
for (int ncase = 0; ncase < T; ncase++)
{
if (ncase > 0) cout << endl;
int N;
vector<vector<int> > shuffles;
vector<int> src;
initialize(N, shuffles, src);
solve(src, shuffles);
print(src);
}
//fclose(stdin);
//fclose(stdout);
return 0;
}