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
|
#include <string.h> #define MAXN 100 char str[MAXN] = {"12341"};
void (char *s, int m) { int len = strlen(s); int all = 1<<len; for(int i = 0; i < all; ++i) { if(__builtin_popcount(i) != m) continue; for(int b = 0; b < 20; ++b) if(i&(1<<b)) putchar(s[b]); putchar('n'); } }
char buf[MAXN]; int mark[300]; void dfs(char *s, int n, int m, int cnt, int dep) { if(dep > n) return; if(cnt == m) { for(int i = 0; i < cnt; ++i) putchar(buf[i]); putchar('n'); return ; } buf[cnt] = str[dep]; if(!mark[str[dep]]) { mark[str[dep]] = 1; dfs(s, n, m, cnt+1, dep+1); mark[str[dep]] = 0; } dfs(s, n, m, cnt, dep+1); }
int main() {
dfs(str, strlen(str), 3, 0, 0); return 0; }
|
近期评论