UVa555 - Bridge Hands

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const char rank[] = "23456789TJQKA";
const char suite[] = "CDSH";

map<char, int> rankMap;
map<char, int> suiteMap;
map<char, int> dirMap;
map<int, char> intMap;

struct Card
{
    char rank;
    char suite;
    bool operator < (const Card & b) const
    {
        if (suiteMap[suite] != suiteMap[b.suite])
            return suiteMap[suite] < suiteMap[b.suite];

        return rankMap[rank] < rankMap[b.rank];   
    }
};



void init();

int main()
{
    string s;
    int cur;
    vector<Card> v[4];
    Card card;

#ifndef ONLINE_JUDGE
    ifstream cin("uva_in.txt");
#endif

    init();

    while (getline(cin, s)) {
        if (s == "#") break;
        
        for (int i = 0; i < 4; i++) v[i].clear();

        cur = dirMap[s[0]];
        string tmp;
        getline(cin, tmp);
        s = tmp;
        getline(cin, tmp);
        s += tmp;

        for (size_t i = 0; i < s.length(); i += 2) {
            card.suite = s[i];
            card.rank = s[i + 1];
             cur = (cur + 1) % 4;
            v[cur].push_back(card);
                   
        }

        for (int i = 0; i < 4; i++) {
            sort(v[i].begin(), v[i].end());
            cout << intMap[i] << ": ";
            for (size_t j = 0; j < v[i].size(); j++) {
                if (j) cout << " ";
                cout << v[i][j].suite << v[i][j].rank;
            }
            cout << endl;
        }
        
    }
    return 0;
}

void init()
{
    int len;

    len = strlen(rank);
    for (int i = 0; i < len; i++) {
        rankMap[rank[i]] = i;
    }

    len = strlen(suite);
    for (int i = 0; i < len; i++) {
        suiteMap[suite[i]] = i;
    }

    dirMap['S'] = 0;
    dirMap['W'] = 1;
    dirMap['N'] = 2;
    dirMap['E'] = 3;

    intMap[0] = 'S';
    intMap[1] = 'W';
    intMap[2] = 'N';
    intMap[3] = 'E';
}

你可能感兴趣的:(UVa555 - Bridge Hands)