PKU 3283 Card Hands

  • カードの順番を逆にしてTrie木を作り、ノードの数を数えて終わりです
  • メモリが足りなかったりTLEしたりで、何どもやり直しました
int main()
{
	const string firstLetters[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
	const string secondLetters[] = {"C","D","H","S"};
	map<string, int> dic;
	for (int i = 0; i < 13; ++i) {
		for (int j = 0; j < 4; ++j) {
			dic[firstLetters[i] + secondLetters[j]] = i + j * 4;
		}
	}

	int numberOfHands;
	while (cin >> numberOfHands && numberOfHands) {
		static map<int, int> trie[128 * 1024];
		int trieSize = 1;
		for (int handIndex = 0; handIndex < numberOfHands; ++handIndex) {
			int numberOfCards;
			cin >> numberOfCards;

			vector<int> cards;
			for (int i = 0; i < numberOfCards; ++i) {
				string card;
				cin >> card;
				cards.push_back(dic[card]);
			}

			reverse(cards.begin(), cards.end());

			int trieIndex = 0;
			for (vector<int>::iterator it = cards.begin(); it != cards.end(); ++it) {
				if (trie[trieIndex].find(*it) == trie[trieIndex].end()) {
					trie[trieIndex][*it] = trieSize++;
				}
				trieIndex = trie[trieIndex][*it];
			}
		}

		cout << trieSize - 1 << endl;
	}
}