#include<cstdio> #include<string.h> using namespace std; int counter=0; void fun(char* str,bool* status,int n,int size,int index,int length_upto){ counter++; if(index==n) return; fun(str,status,n,size,index+1,length_upto); status[index]=true; length_upto++; if(length_upto==size){ for(int i=0;i<n;i++){ if(status[i]==true){ printf("%c",str[i]); } } printf(" "); status[index]=false; return; } else{ fun(str,status,n,size,index+1,length_upto); status[index]=false; } } int main(){ char str[100]; int n; bool* status; scanf("%s",str); n=strlen(str); status=new bool[n]; for(int i=0;i<n;i++){ status[i]=false; } for(int i=1;i<=n;i++){ printf("Length %d---->>>>\n",i); fun(str,status,n,i,0,0); printf("\n"); } printf("%d",counter); return 0; }
123456789
Length 1---->>>> 9 8 7 6 5 4 3 2 1 Length 2---->>>> 89 79 78 69 68 67 59 58 57 56 49 48 47 46 45 39 38 37 36 35 34 29 28 27 26 25 24 23 19 18 17 16 15 14 13 12 Length 3---->>>> 789 689 679 678 589 579 578 569 568 567 489 479 478 469 468 467 459 458 457 456 389 379 378 369 368 367 359 358 357 356 349 348 347 346 345 289 279 278 269 268 267 259 258 257 256 249 248 247 246 245 239 238 237 236 235 234 189 179 178 169 168 167 159 158 157 156 149 148 147 146 145 139 138 137 136 135 134 129 128 127 126 125 124 123 Length 4---->>>> 6789 5789 5689 5679 5678 4789 4689 4679 4678 4589 4579 4578 4569 4568 4567 3789 3689 3679 3678 3589 3579 3578 3569 3568 3567 3489 3479 3478 3469 3468 3467 3459 3458 3457 3456 2789 2689 2679 2678 2589 2579 2578 2569 2568 2567 2489 2479 2478 2469 2468 2467 2459 2458 2457 2456 2389 2379 2378 2369 2368 2367 2359 2358 2357 2356 2349 2348 2347 2346 2345 1789 1689 1679 1678 1589 1579 1578 1569 1568 1567 1489 1479 1478 1469 1468 1467 1459 1458 1457 1456 1389 1379 1378 1369 1368 1367 1359 1358 1357 1356 1349 1348 1347 1346 1345 1289 1279 1278 1269 1268 1267 1259 1258 1257 1256 1249 1248 1247 1246 1245 1239 1238 1237 1236 1235 1234 Length 5---->>>> 56789 46789 45789 45689 45679 45678 36789 35789 35689 35679 35678 34789 34689 34679 34678 34589 34579 34578 34569 34568 34567 26789 25789 25689 25679 25678 24789 24689 24679 24678 24589 24579 24578 24569 24568 24567 23789 23689 23679 23678 23589 23579 23578 23569 23568 23567 23489 23479 23478 23469 23468 23467 23459 23458 23457 23456 16789 15789 15689 15679 15678 14789 14689 14679 14678 14589 14579 14578 14569 14568 14567 13789 13689 13679 13678 13589 13579 13578 13569 13568 13567 13489 13479 13478 13469 13468 13467 13459 13458 13457 13456 12789 12689 12679 12678 12589 12579 12578 12569 12568 12567 12489 12479 12478 12469 12468 12467 12459 12458 12457 12456 12389 12379 12378 12369 12368 12367 12359 12358 12357 12356 12349 12348 12347 12346 12345 Length 6---->>>> 456789 356789 346789 345789 345689 345679 345678 256789 246789 245789 245689 245679 245678 236789 235789 235689 235679 235678 234789 234689 234679 234678 234589 234579 234578 234569 234568 234567 156789 146789 145789 145689 145679 145678 136789 135789 135689 135679 135678 134789 134689 134679 134678 134589 134579 134578 134569 134568 134567 126789 125789 125689 125679 125678 124789 124689 124679 124678 124589 124579 124578 124569 124568 124567 123789 123689 123679 123678 123589 123579 123578 123569 123568 123567 123489 123479 123478 123469 123468 123467 123459 123458 123457 123456 Length 7---->>>> 3456789 2456789 2356789 2346789 2345789 2345689 2345679 2345678 1456789 1356789 1346789 1345789 1345689 1345679 1345678 1256789 1246789 1245789 1245689 1245679 1245678 1236789 1235789 1235689 1235679 1235678 1234789 1234689 1234679 1234678 1234589 1234579 1234578 1234569 1234568 1234567 Length 8---->>>> 23456789 13456789 12456789 12356789 12346789 12345789 12345689 12345679 12345678 Length 9---->>>> 123456789 5110