상세 컨텐츠

본문 제목

백준 1759 암호 만들기 (C++)

baekjoon

by nownow 2024. 2. 19. 05:19

본문

https://www.acmicpc.net/problem/1759

목록에서 필요한 갯수만큼 조건에 맞춰 탐색해 뽑아낸다.

백트래킹 사용해야 했던 문제

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cmath>
using namespace std;
char str[20];
char arr[20];
int l, c;
bool check[26];
void dfs(int index, int count);
int main()
{
	memset(check, 0, sizeof(check));
	check[0] = check[4] = check['i' - 'a'] = check['o' - 'a']= check['u' - 'a'] = 1;
	cin >> l >> c;
	for (int i = 0; i < c; i++)
	{
		cin >> arr[i];
	}
	sort(arr, arr + c);
	dfs(0, 0);
}
void dfs(int index, int count)
{
	if (count == l || index == c) return;

	str[count] = arr[index];
	dfs(index + 1, count + 1);//한칸씩 밀면서 출력할 문자열에 하나씩 추가해주다가

	if (count == l - 1)//출력할 자릿수 모두 채웠으면 검사후 조건맞을시 출력
	{
		int jj =0, mm = 0;
		for (int i = 0; i < l; i++)
		{
			if (check[str[i] - 'a'] == 1)mm++;
			else jj++;
		}
		if(jj>=2 && mm>=1)puts(str);
	}

	dfs(index + 1, count);
    //끝까지갔으면 백트래킹으로 한칸돌아와서 기존것보다 뒤에 글자를 집어넣어서 문자열 만든다.
}

 

'baekjoon' 카테고리의 다른 글

백준 11000 강의실배정 (C++)  (0) 2024.02.27
백준 6603 로또 (c++/백트래킹)  (0) 2024.02.26
백준 1697 숨바꼭질 (C++)  (0) 2024.02.18
백준 1932 정수삼각형 (C++)  (0) 2024.02.17
백준 11726 2xn 타일링(C++)  (0) 2024.02.17

관련글 더보기