-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktracking.cpp
More file actions
62 lines (60 loc) · 1.05 KB
/
Copy pathBacktracking.cpp
File metadata and controls
62 lines (60 loc) · 1.05 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//backtracking 10776
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<iostream>
using namespace std;
#define s scanf
#define p printf
int j,m,n;
char arr[100];
bool taken[100];
vector <char> result;
void call (void)
{
int i;
if(result.size()==m)
{
for(i=0;i<m;++i)
cout<<result[i];
cout<<endl;
}
else
{
for(i=0;i<n;++i)
{
if(!result.empty()&&result[result.size()-1]>arr[i])
continue;
if(taken[i]==0)
{
taken[i]=1;
result.push_back(arr[i]);
call();
taken[i]=0;
result.pop_back();
while(arr[i]==arr[i+1])
++i;
}
}
}
}
int main()
{
while(s("%s %d",arr,&m)==2)
{
n=strlen(arr);
sort(arr,arr+n);
call();
memset(arr,0,sizeof(arr));
memset(taken,0,sizeof(taken));
result.clear();
}
return 0;
}
/*
abcde 2
abcd 3
aba 2
*/