結果

問題 No.239 にゃんぱすー
ユーザー tkr987tkr987
提出日時 2018-07-29 18:22:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 2,959 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 81,892 KB
実行使用メモリ 4,428 KB
最終ジャッジ日時 2023-09-25 11:37:18
合計ジャッジ時間 2,275 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 6 ms
4,384 KB
testcase_22 AC 6 ms
4,380 KB
testcase_23 AC 7 ms
4,384 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 8 ms
4,384 KB
testcase_26 AC 9 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 4 ms
4,380 KB
testcase_33 AC 5 ms
4,380 KB
testcase_34 AC 7 ms
4,376 KB
testcase_35 AC 8 ms
4,376 KB
testcase_36 AC 9 ms
4,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

/**
@breif にゃんぱす格納関数
@param collection 格納する挨拶コンテナ
@note
 入力を1行だけコンテナに格納する。
 vectorは村民1に対する相手jの挨拶が格納される。
 mapには村民jの村民1相手以外の挨拶が格納される。
 つまり、vectorが挨拶行列の列、mapが挨拶行列の行を指す。
**/
void InputToCollection(vector<multimap<string, int>>* collection)
{
	for (int i = 0; i < (int)collection->size(); i++)
	{	// mapのキーのみ使用する、mapの値は使用しないので適当な数字を入れておく
		string word; cin >> word;
		(*collection)[i].emplace(word, 0);
	}
}

/**
@breif にゃんぱす検索関数
@param collection 検索する村人の挨拶コンテナ
@retval [1-INT_MAX] れんちょんの村民番号
@retval -1 れんちょんを特定できない
@note
 この関数はテストケースの穴を突いている。
 たとえば
 - nyanpass ohayo
 a - chicchakunaiyo
 nyanpass nyanpass -
 この場合、村民1は村民2に対してnyanpassと挨拶してないので-1が正解な気がする。
 しかし、nyanpassより値の小さいテストケースが存在しないのかmapの最後尾だけ調べてもACが通った。
 より正確な検索関数はFindNyanpassII()で実装。
**/
int FindNyanpass(vector<multimap<string, int>>* collection)
{
	int find = -1;
	// れんちょんは誰に対しても"nyanpass"と挨拶するので
	// mapの最後尾(map::endの1つ前の要素)だけ調べて判断する
	for (int nyan_index = 0; nyan_index < (int)collection->size(); nyan_index++)
	{	
		if ((--(*collection)[nyan_index].end())->first == "nyanpass")
		{
			if (find == -1)
				find = nyan_index + 1;
			else
			{
				find = -1;
				break;
			}
		}
	}

	return find;
}

/**
@breif にゃんぱす検索関数II
@param collection 検索する村人の挨拶コンテナ
@retval [1-INT_MAX] れんちょんの村民番号
@retval -1 れんちょんを特定できない
**/
int FindNyanpassII(vector<multimap<string, int>>* collection)
{
	int find = -1;
	// れんちょんは誰に対しても"nyanpass"と挨拶するので
	// "nyanpass"キーが村民-1個存在するか調べて判断する
	for (int nyan_index = 0; nyan_index < (int)collection->size(); nyan_index++)
	{
		if ((*collection)[nyan_index].count("nyanpass") == collection->size() - 1)
		{
			if (find == -1)
				find = nyan_index + 1;
			else
			{
				find = -1;
				break;
			}
		}
	}

	return find;
}


int main()
{
	vector<multimap<string, int>> nya_collection;
	int nya_max; cin >> nya_max;
	nya_collection.resize(nya_max);

	// nya_max 繰り返して全ての入力をコンテナに格納
	for (int i = 0; i < nya_max; i++)
		InputToCollection(&nya_collection);

	cout << FindNyanpass(&nya_collection);
//	cout << FindNyanpassII(&nya_collection);

	return 0;
}
0