[백준] 1620 나는야 포켓몬 마스터

leejihun·2022년 5월 20일
0

알고리즘

목록 보기
4/50

map 사용하여 간단하게 풀이가능
atoi, stoi -> 문자열을 숫자로 (stoi가 더빠르다함 c++11 이후로는)

to_string -> 숫자를 문자열로
string to char* => c_str()사용


int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);


	int n = 0, m = 0;

	vector<string> name;
	map<string, int> m1;

	vector<string> ans;
	vector<string> tmp; // 입력받을예정


	cin >> n >> m;

	for (int i = 1; i <= n; i++)
	{
		string s1;
		cin >> s1;

		name.push_back(s1);
		m1.insert(make_pair(s1, i));

	}

	for (int i = 0; i < m; i++)
	{
		string s2;
		cin >> s2;

		if (s2[0] >= 65 && s2[0] <= 90) //앞글자 대문자이면 
		{
			ans.push_back(to_string(m1[s2]));
		}

		else
		{
			ans.push_back(name[stoi(s2) - 1]);
		}
	}
	for (int i = 0; i < ans.size(); i++) {
		cout << ans[i] << '\n'; // 출력
	}
	return 0;
}
profile
U+221E

0개의 댓글