백준 algorithm
백준 1759 - 암호만들기
cosmohoo
2020. 6. 17. 15:44
반응형
=> 입력값의 범위가 작기 때문에 브루트 포스로 해결할 수 있습니다.
** 본인이 작성한 코드에는 정렬의 과정이 표현되어있지 않기 때문에, 함수 진입 전에 alpha vector의 sort과정이 필요합니다.
<code>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
bool check(string &password)
{
int conso=0;
int vowel=0;
for(char x : password)
{
if(x == 'a' || x=='e' ||x=='i' || x == 'o' || x== 'u')
{
vowel ++;
}
else{
conso++;
}
}
return conso >= 2 && vowel >=1;
}
void go(int n, vector<char> &alpha, string password, int i)
{
if(password.length() == n)
{
if(check(password))
{
cout << password<<'\n';
}
return;
}
if(i >= alpha.size())return;
go(n, alpha, password+alpha[i], i+1);
go(n, alpha, password, i+1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int L, C;
cin >> L >> C;
vector<char> alpha;
string password;
int cnt =0;
for(int i=0; i<C; i++)
{
char c;
cin >> c;
alpha.push_back(c);
}
sort(alpha.begin(), alpha.end());
go(L, alpha, password, 0);
return 0;
}
반응형