백준 9536 c++

magicdrill·2024년 7월 23일
0

백준 문제풀이

목록 보기
400/654

백준 9536 c++

공백 포함 문자열을 입력받아서 ' '을 기준으로 나눠서 검사를 진행하는 문제이다. 검사를 진행하는 과정에서 가장 마지막에는 ' '가 없으므로 마지막 단어는 검사를 안하게 되므로 따로 검사를 해야 한다.

#include <iostream>
//#include <vector>
#include <set>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

void input_sound_data(string &entire_sound, set<string>& animal_sound/*vector<string> &animal_sound*/)
{
	string animal_sound_data;
	int i, j;

	//cin.ignore();
	getline(cin, entire_sound);//공백포함 녹음기에 저장된 모든 음성 정보 입력
	//cout << entire_sound << "\n";
	while (1)
	{
		getline(cin, animal_sound_data);
		if (animal_sound_data == "what does the fox say?")
		{
			break;
		}
		//cout << animal_sound_data << "\n";
		for (i = animal_sound_data.length() - 1; i >= 0; i--)
		{
			if (animal_sound_data[i] == ' ')
			{
				//animal_sound.push_back(animal_sound_data.substr(i + 1));
				animal_sound.insert(animal_sound_data.substr(i + 1));
				break;
			}
		}
	}
	/*for (string temp : animal_sound)
	{
		cout << temp << " ";
	}
	cout << "\n";*/

	return;
}

void find_answer(string& entire_sound, set<string> &animal_sound/*vector<string>& animal_sound*/)
{
	int i, j = 0;
	string temp;
	bool search = false;
	//마지막 단어를 조사를 안해서 오류 발생

	for (i = 0; i < entire_sound.length(); i++)
	{
		search = false;
		if (entire_sound[i] == ' ')
		{
			temp = entire_sound.substr(j, i - j);
			for (string find : animal_sound)
			{
				if (find == temp)
				{
					search = true;
					break;
				}
			}
			if (!search)
			{
				cout << temp << " ";
			}
			j = i + 1;
		}
	}
	//마지막 단어도 검사
	temp = entire_sound.substr(j);
	for (string find : animal_sound)
	{
		if (find == temp)
		{
			search = true;
			break;
		}
	}
	if (!search)
	{
		cout << temp << " ";
	}
	cout << "\n";

	return;
}

void test_case()
{
	int i, t;

	cin >> t;
	cin.ignore();
	for (i = 0; i < t; i++)
	{
		string entire_sound;
		//vector<string> animal_sound;
		set<string> animal_sound;

		input_sound_data(entire_sound, animal_sound);
		find_answer(entire_sound, animal_sound);
	}

	return;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	test_case();

	return 0;
}

0개의 댓글