※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 13908번 문제인 비밀번호이다.
문제는 아래 링크를 확인하자.

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

 

13908번: 비밀번호

첫 번째 예제의 경우 가능한 비밀번호의 조합은 07, 17, 27, 37, 47, 57, 67, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 87, 97이다. 두 번째 예제의 경우 가능한 비밀번호의 조합은 34, 43이다.

www.acmicpc.net

0부터 9999999까지의 수는 총 1000만개이므로, 모든 수에 대해 주어진 "들어있어야 하는 수"가 각각 들어있는지를 확인하는 브루트포스를 통해 문제를 해결할 수 있다.

 

to_string 함수를 이용해 정수를 문자열로 바꿔 편하게 구현하자.

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;

int visited[128];
vector<char> needed;

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

	int ans = 0;
	int N, M; cin >> N >> M;
	while (M--) {
		char c; cin >> c;
		needed.emplace_back(c);
	}

	int iterend = 1;
	for (int i = 0; i < N; i++) iterend *= 10;
	int lessdigit = iterend / 10;
	for (int i = 0; i < iterend; i++) {
		bool chk = 1;
		memset(visited, 0, sizeof(visited));
		if (i < lessdigit) visited['0'] = 1;
		string s = to_string(i);
		for (auto& l : s) visited[l] = 1;
		for (auto& l : needed) {
			if (!visited[l]) chk = 0;
		}

		if (chk) ans++;
	}

	cout << ans;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 13910 // C++] 개업  (0) 2023.03.23
[BOJ 13909 // C++] 창문 닫기  (0) 2023.03.22
[BOJ 25938 // C++] Towers of Hanoi Grid  (0) 2023.03.20
[BOJ 25937 // C++] Balanced Strings  (0) 2023.03.19
[BOJ 25895 // C++] Don't Break the Ice  (0) 2023.03.18

+ Recent posts