nper.c
1.25 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
63
64
65
66
67
68
69
70
71
72
#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 );
}