nper.c 1.25 KB
#include <stdio.h>
#include <strings.h>

#define BUFLEN (256)

#define MAXPERIOD (256*256)

char *strs[MAXPERIOD];

main()
{
    int nonp, per, num, indx, ntot;
    int i;
    char buf[BUFLEN];

    per = 0;
    num = 1;
    indx = 0;
    nonp = 1;
    ntot = 1;

    fgets( buf, BUFLEN, stdin );

    strs[0] = strdup( buf );

    while( fgets( buf, BUFLEN, stdin ) != NULL )
	{

	    if( per && (strcmp( buf, strs[ ntot - num*per ] ) == 0) ) {
					/* Match at the expected location */
		strs[ntot] = strs[ ntot - num*per ];
		indx++;
		if( indx == per ) {
		    indx = 0;
		    num++;
		};
	    } else {		/* Check for a match beyond periodic section */
		for(i=1; i<=ntot; i++)
		    if( strcmp( buf, strs[ ntot-i ] ) == 0 ) {
			per = i;
			num = 1;
			indx = 1;
			nonp = ntot - i;
			strs[ ntot ] = strs[ ntot-i ];
			break;
		    };
		
		if( i > ntot ) {
		    strs[ ntot ] = strdup( buf );
		    per = 0;
		    num = 1;
		    indx = 0;
		    nonp = ntot;
		};

	    };

	    printf("nonp= %d, period= %d, nreps= %d, indx= %d, ntot= %d\n",
		    nonp, per, num, indx, ntot);
	    
	    ntot++;
    }

#define NPAD	((ntot>>3)+8)

    num = nonp + 2*per;
    if( num >= (ntot - NPAD) )
	num = ntot - NPAD;
    printf("Sim length = %d + 2 * %d = %d\n", nonp, per, num );
}