Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 버킷 정렬
- dfs
- c#
- 게리멘더링2
- 백준 1406
- qorwns
- 백준 17822
- 1764
- 원판 돌리기
- 스택의 특징
- 조세퍼스 순열
- AVL 시간 복잡도
- ㅣ풀이
- 백준 5397
- heap
- 백준 17779
- 5397
- 해시 구현
- 풀이
- 구현
- 백준 1158
- 자료구조
- 백준 2447
- 별 찍기 10
- Stack 이란
- C/C++ 구현
- 백준
- 백준 17471
- 해시구현
- 시간 복잡도
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 15650] N과 M(2) 본문
요구사항
중복을 포함하지 않고 순열을 구현하여라
풀이
N과 M이 작아서 전부다 탐색해도 풀린다
#include <stdio.h>
#include <algorithm>
#include <list>
//#include "Windows.h"
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int PN=23;
const int HASHSIZE=12371;
int table[HASHSIZE][50];
char hash_table[HASHSIZE][100];
int hash_size=0;
unsigned int get_key(char str[]){
unsigned int key=0, p=1;
for(int i=0 ; str[i]!=0; i++){
key +=(str[i]*p);
p*=PN;
}
return key%HASHSIZE;
}
void add(char str[]){
unsigned int key=get_key(str);
int len=0;
for(len=0; str[len]!=0; len++){
hash_table[hash_size][len]=str[len];
}
hash_table[hash_size][len]=0;
int &size=table[key][0];
table[key][++size]=hash_size++;
}
int str_cmp(char a[], char b[]){
int i=0,j=0;
while(a[i]){
if(a[i++]!=b[j++]){
i--,j--;
break;
}
}
return a[i]-b[j];
}
int contain(char str[]){
unsigned int key=get_key(str);
int size=table[key][0];
for(int i=1; i <=size; i++){
int pos=table[key][i];
if(str_cmp(hash_table[pos],str) ==0){
return pos;
}
}
return -1;
}
bool remove(char str[]){
unsigned int key=get_key(str);
int &size=table[key][0];
for(int i=1; i <=size ; i++){
int pos=table[key][i];
if(str_cmp(hash_table[pos],str)==0){
for(int j=i; j<size ; j++){
table[key][j]=table[key][j+1];
}
size--;
return true;
}
}
return false;
}
int make_int(int min,int max){
return rand()%(max-min)+min;
}
char make_char(){
int val=rand()%52;
if(val<26){
return static_cast<char>('a'+val);
}
return static_cast<char>('A'+val-26);
}
map<char*,int> test;
char input[12371][120];
int n,m;
vector<int> v;
int a[100];
int visit[100];
void dfs(int now,int cnt){
if(cnt==m){
for(int i=0 ; i <v.size();i++){
printf("%d ",v[i]);
}
printf("\n");
return;
}
for(int i = now; i < n ; i++){
if(visit[i]==1) continue;
visit[i]=1;
v.push_back(a[i]);
dfs(i+1,cnt+1);
visit[i]=0;
v.pop_back();
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0 ; i <n ; i++){
a[i]=i+1;
}
dfs(0,0);
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 15652]N과 M(4) (0) | 2020.03.12 |
---|---|
[백준 15651] N과 M(3) (0) | 2020.03.12 |
[백준 15649] N과 M(1) (0) | 2020.03.12 |
[백준 1041] 주사위 (0) | 2020.03.12 |
[백준 1205] 등수 구하기 (0) | 2020.03.11 |
Comments