๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

1759๋ฒˆ: ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ L, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (3 ≤ L ≤ C ≤ 15) ๋‹ค์Œ ์ค„์—๋Š” C๊ฐœ์˜ ๋ฌธ์ž๋“ค์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์ด๋ฉฐ, ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์€ ์—†๋‹ค.

www.acmicpc.net

 

๐Ÿง‘๐Ÿป‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

  1. ์ž…๋ ฅ์—์„œ ๋ชจ์Œ๊ณผ ์ž์Œ์„ ๊ฐ๊ฐ ๋ถ„๋ฆฌํ•˜์—ฌ ๋”ฐ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  2. ์•„๋ž˜์˜ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋ฉด์„œ, ์ €์žฅํ•œ ๋ชจ์Œ๊ณผ ์ž์Œ์„ ์กฐํ•ฉํ•˜์—ฌ ๊ธธ์ด๊ฐ€ L์ด ๋  ๋•Œ๊นŒ์ง€ ์•”ํ˜ธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
    • ๋ชจ์Œ์€ ์ตœ์†Œ 1๊ฐœ ~ ์ตœ๋Œ€ L - 2๊ฐœ๊นŒ์ง€ ์•”ํ˜ธ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์ž์Œ์€ ์ตœ์†Œ 2๊ฐœ ~ ์ตœ๋Œ€ L - 1๊ฐœ๊นŒ์ง€ ์•”ํ˜ธ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. ์ƒ์„ฑํ•œ ์•”ํ˜ธ์˜ ๊ธธ์ด๊ฐ€ L์ด ๋˜์—ˆ๋‹ค๋ฉด, ์•”ํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
  4. ์ค‘๋ณต์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด, ์ •๋ ฌํ•œ ์•”ํ˜ธ๋ฅผ set ์ž๋ฃŒ๊ตฌ์กฐ์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  5. ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜์— ๋Œ€ํ•ด ๋‹ค ์กฐํ•ฉ ํ•˜์˜€๋‹ค๋ฉด, ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

๋ชจ์Œ๊ณผ ์ž์Œ์ด ๊ฐ๊ฐ ํฌํ•จ๋˜์–ด์•ผ ํ•  ๊ตฌ๊ฐ„๊ฐ’์ด ์ •ํ•ด์ ธ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ถ€๋ถ„์„ ์•”ํ˜ธ ์ƒ์„ฑ์—์„œ ์ถ”๊ฐ€ ์กฐ๊ฑด์œผ๋กœ ๊ฑธ์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

โœ๏ธ์†Œ์Šค ์ฝ”๋“œ ๋ฐ ๊ฒฐ๊ณผ

#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;

class Decoder
{
public:
	Decoder(int L, int C);
	void FindPasswords();
	void ShowPasswords();
private:
	void FindCombination(string password, int vowelIdx, int consonantIdx, int vowelCount, int consonantCount);
private:
	const set<char> VOWEL{ 'a', 'e', 'i', 'o', 'u' };
	string _vowels;
	string _consonants;
	set<string> _answers;
	int _L;
};

Decoder::Decoder(int L, int C)
{
	_L = L;

	for (int i = 0; i < C; i++)
	{
		char alphabet;
		cin >> alphabet;

		auto FindIter = VOWEL.find(alphabet);
		FindIter == VOWEL.end() ? _consonants.push_back(alphabet) : _vowels.push_back(alphabet);
	}
}

void Decoder::ShowPasswords()
{
	for (const auto& answer : _answers)
		cout << answer << "\n";
}

void Decoder::FindCombination(string password, int vowelIdx, int consonantIdx, int vowelCount, int consonantCount)
{
	if (password.length() == _L)
	{
		sort(password.begin(), password.end());
		_answers.emplace(password);
		return;
	}

	// ๋ชจ์Œ์€ ์ตœ์†Œ 1๊ฐœ๋ถ€ํ„ฐ ์ตœ๋Œ€ L-2๊ฐœ๊นŒ์ง€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ
	for (int vi = vowelIdx; vi < _vowels.size() and vowelCount < _L - 2; vi++)
	{
		password.push_back(_vowels[vi]);
		FindCombination(password, vi + 1, consonantIdx, vowelCount + 1, consonantCount);
		password.pop_back();
	}

	// ์ž์Œ์€ ์ตœ์†Œ 2๊ฐœ๋ถ€ํ„ฐ ์ตœ๋Œ€ L-1๊ฐœ๊นŒ์ง€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Œ
	for (int ci = consonantIdx; ci < _consonants.size() and consonantCount < _L - 1; ci++)
	{
		password.push_back(_consonants[ci]);
		FindCombination(password, vowelIdx, ci + 1, vowelCount, consonantCount + 1);
		password.pop_back();
	}
}

void Decoder::FindPasswords()
{
	string password = "";
	FindCombination(password, 0, 0, 0, 0);
}

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

	int L, C;
	cin >> L >> C;

	Decoder decoder(L, C);
	decoder.FindPasswords();
	decoder.ShowPasswords();
	return 0;
}

 

๊ด€๋ฆฌํ•ด์•ผ ํ•  ๋ฐ์ดํ„ฐ๋“ค์ด ๋งŽ์•„์ง€๋‹ˆ, ํด๋ž˜์Šค ํŒŒ์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒŒ ์—ญ์‹œ ํŽธํ•˜๋„ค์š”.

 

 

728x90
๋ฐ˜์‘ํ˜•