Implementation of non-recursive C language with full permutation and total combination

Source: Internet
Author: User
Tags strlen

/***perm.h*****/

#ifndef _algorithm_perm_h
#define _algorithm_perm_h

#include <windows.h>

The callback function, and the result of each permutation or combination is passed to the function as a parameter
typedef void (*PERM_CALLBACK_PROC) (int nindexarr[], int narrlen, LPVOID lpuserdata);


/**************************************************************************************

Function Name: Permnorecurse
function function: Use the non-recursive algorithm to arrange the elements in an array, and access the array elements in each permutation
Parameter list:
Number of narrsize array elements
Lpuserdata point to the first address of the user array
Call_back the callback function that executes each permutation result
return value: None
Note: Each permutation result is passed to the callback function Call_back execution

***************************************************************************************/

void Permnorecurse (int narrsize, LPVOID lpuserdata, Perm_callback_proc call_back);

Find all combinations of M elements from the array A[1..N].
A[1..N] Represents a candidate set, and M represents the number of elements in a combination.
Returns the total number of all combinations.
/**************************************************************************************

Function Name: Combinenorecurse
function function: Use the non recursive backtracking method to select m from an array containing n elements
Parameter list:
Total number of narrsize array elements
Number of elements in a ncombine combination
Lpuserdata point to the first address of the user array
Call_back the callback function that executes each combination result
return value: None
Note: Each combination result is passed to the callback function Call_back execution

***************************************************************************************/
void Combinenorecurse (int narrsize, int ncombine, LPVOID lpuserdata, Perm_callback_proc call_back);


#endif

/*****perm.cpp*****/

#include "Perm.h"

Swap the values of the two elements labeled I and J in the array a
void swap (int* a,int i,int J)
{
A[i] ^= a[j];
A[J] ^= a[i];
A[i] ^= a[j];
}

Reverse all elements of the subscript I to subscript j in Array a
void reverse (int a[],int I,int j)
{
for (; i<j; ++i,--J)
{
Swap (A,I,J);
}
}


void Permnorecurse (int narrsize, LPVOID lpuserdata, Perm_callback_proc call_back)
{
if (narrsize<2)
{
Return
}

int i = 0;
int j = 0;
int * Pindexarr = new Int[narrsize];
for (i=narrsize-1; i>=0; i--)
{
Pindexarr[i] = i;
}

Todo
{
Call_back (Pindexarr, Narrsize, Lpuserdata);

Find subscript I for elements that do not conform to the trend
for (i=narrsize-2; i>=0;-I.)
{
if (pindexarr[i]<pindexarr[i+1])
{
Break
}
Else
{
if (i = = 0)
{
Return
}
}
}


for (j=narrsize-1; j>i;--j)
{
if (Pindexarr[j]>pindexarr[i])
{
Break
}
}

Swap (PINDEXARR,I,J);

Reverse (pindexarr,i+1,narrsize-1);
}while (TRUE);

delete []pindexarr;
}


void Combinenorecurse (int narrsize, int ncombine, LPVOID lpuserdata, Perm_callback_proc call_back)
{
Ncombine = ncombine > narrsize? Narrsize:ncombine;

int * Pindexarr = new Int[ncombine + 1];
for (int i=0; i<=ncombine; i++)
{
Pindexarr[i] = i-1; Note Here order[0]=-1 is used as a loop to determine the identity
}

int k = Ncombine;
BOOL flag = TRUE; Flag to find a valid combination
while (pindexarr[0] = =-1)
{
if (flag)//output meets the required combination
{
/*for (i=1; i<=m; i++)
cout << A[order[i]] << "";
Call_back (&pindexarr[1], ncombine, lpuserdata);
Flag = false;
}

pindexarr[k]++; Select a new number at the current location
if (pindexarr[k] = = narrsize)//Current position has no number to choose from, backtracking
{
pindexarr[k--] = 0;
Continue
}

if (K < ncombine)//update the number at the next position in the current position
{
PINDEXARR[++K] = pindexarr[k-1];
Continue
}

if (k = = Ncombine)
Flag = true;
}

Delete[] Pindexarr;
}

/*
int combine (int a[], int n, int m)
{
m = m > n? n:m;

int* order = new int[m+1];
for (int i=0; i<=m; i++)
Order[i] = i-1; Note Here order[0]=-1 is used as a loop to determine the identity

int count = 0;
int k = m;
BOOL flag = TRUE; Flag to find a valid combination
while (order[0] = =-1)
{
if (flag)//output meets the required combination
{
for (I=1; i<=m; i++)
cout << A[order[i]] << "";
cout << Endl;
count++;
Flag = false;
}

order[k]++; Select a new number at the current location
if (order[k] = n)//Current position has no number to choose from, backtracking
{
order[k--] = 0;
Continue
}

if (K < m)//update the number at the next position in the current position
{
ORDER[++K] = order[k-1];
Continue
}

if (k = = m)
Flag = true;
}

Delete[] Order;
return count;
}
*/

/********testperm.cpp********/

TestPerm.cpp:Defines the entry point for the console application.
//

#include "stdafx.h"
#include <time.h>
#include "Perm.h"

Long G_ncount = 0;

void Processonecomb (int nindexarr[], int narrlen, LPVOID lpuserdata)
{
char * ppass = (char *) lpuserdata;
printf ("(");
for (int i=0; i<narrlen-1; i++)
{
printf ("%c,", Ppass[nindexarr[i]);
}
printf ("%c),", Ppass[nindexarr[i]]);
printf ("\ n");
}

void processoneperm (int nindexarr[], int narrlen, LPVOID lpuserdata)
{
char * ppass = (char *) lpuserdata;
for (int i=0; i<narrlen; i++)
{
printf ("%c", Ppass[nindexarr[i]);
}
if (strnicmp ("Robin", Ppass, 5) = = 0)
{
GetChar ();
}
printf ("\ n");
}

void Processoneitem (int nindexarr[], int narrlen, LPVOID lpuserdata)
{
G_ncount + +;

Permnorecurse (Narrlen, Lpuserdata, processoneperm);
}

void Processoneitemnull (int nindexarr[], int narrlen, LPVOID lpuserdata)
{
G_ncount + +;
}

int main (int argc, char* argv[])
{
Char szpass[32];
strcpy (Szpass, "abcdefghijklmnopqrstuvwxyz");

int n = 0;

 
 n = 5;
 time_t T_start =  time (NULL);
 combinenorecurse (strlen (Szpass), N, Sz Pass, Processoneitem);
 time_t t_end = time (NULL);
 printf ("Comb (%d,%d) time consumed:%d secs, count =%d \ n", strlen (Szpass), N, T_end-t_start, g_ncount);
 
 //combinenorecurse (strlen (Szpass), N, Szpass, Processonecomb);
 /*
 time_t t_ Start =  time (NULL);
 for (int i=1; i<=26; i++)
 {
  combinenorecurse (strlen (Szpass), I, Szpass, Processoneitem);//); Processoneitemnull
  //printf ("\ n");
 }
 time_t t_end = time (NULL);
 printf ( "Time consumed:%d secs, count =%d \ n",  T_end-t_start, G_ncount);
*/
 


/*
int ncnt = G_ncount;
T_start = time (NULL);
for (int i=1; i<ncnt; i++)
{
Permnorecurse (n, NULL, Processoneitem);
if (i%2000 = 0)
{
printf ("%d full permutation processed.\n", i);
}
}
T_end = time (NULL);
printf ("Perm (%d) time consumed:%d secs, count =%d \ n", N., T_end-t_start, G_ncount);
*/

return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.