#include #include #include #include #include #include #include struct node { struct node *t[6]; }; typedef struct node node; node *r; int i, j, k, l, t, a_o, lim, mot[100000], b, len[100000], **tmp, tt; char mode[100], name[100]; FILE *out; node *new_node(){ int y; node *p = (node*)malloc(sizeof(node)); for(y = 0; y < a_o; y++) p->t[y] = 0; return p; } void load(){ FILE *in = fopen("forbidden.txt", "r"); char ch[3000]; tmp = (int**)malloc(10000*sizeof(int*)); for(fgets(ch, 2000, in); strcmp(mode, ch); fgets(ch, 2000, in), ch[strlen(ch)-1] = 0); for(fgets(ch, 2000, in), k = 0; strcmp(ch, "\n"); fgets(ch, 2000, in), k++){ tmp[k] = (int*)malloc((len[k] = strlen(ch)-1)*sizeof(int)); for(j = 0; j < len[k]; j++) tmp[k][j] = ch[j]-'0'; } fclose(in); }//*/ int ch_cr(int *m, int l){ node *p = r; int x; for(x = 0; x < l; x++) if(!(p = p->t[m[x]])) return 0; return 1; } void add_cr(int *m){ node *p = r; int x; for(x = 0; x < l; x++){ if(!(p->t[m[x]])) p->t[m[x]] = new_node(); p = p->t[m[x]]; } } int eq(int *g, int *d, int l){ for(; l--; ) if(g[l] != d[l]) return 0; return 1; } int square(int *m, int i, int l){ for(; 2*l <= i; l++) if(eq(m+i-2*l, m+i-l, l)) return 1; return 0; }//*/ int right(){ int s; if(square(mot, i+1, t)) return 0; for(s = 0; s < k; s++) if(i+1 >= len[s] && eq(tmp[s], mot+i+1-len[s], len[s])) return 0; return 1; } int check(){ r = new_node(); for(*mot = i = 0; ; ) if(!right()){ here: while(mot[i] == a_o-1){i--; if(i < 0) return 0;} mot[i]++; }else{ if(i == (tt+1)*l-1 && ch_cr(mot+tt*l, l)) goto here; if(i == (2*tt+1)*l-1){ add_cr(mot+tt*l); i = (tt+1)*l-1; goto here; } mot[++i] = 0; } } int display(){ sprintf(name, "%s_pat_%d_%d.txt", mode, tt, l); out = fopen(name, "wt"); for(*mot = i = 0; i >= 0;) if(ch_cr(mot, i+1)) if(i == l-1){ for(j = 0; j <= i; j++) fprintf(out, "%d", mot[j]); fprintf(out, "\n"); there: while(mot[i] == a_o-1) i--; if(i >= 0) mot[i]++; }else mot[++i] = 0; else goto there; fprintf(out, "\n"); close(out); } void usage(){ printf("Usage: pat mode tt l\n"); exit(0); } int main(int ac, char **av){ if(ac != 4) usage(); sprintf(mode, "%s", av[1]); tt = atoi(av[2]); l = atoi(av[3]); if(!strcmp(mode, "f5")){ a_o = 5; t = 1; } else if(!strcmp(mode, "m1")){ a_o = 3; t = 1; } else if(!strcmp(mode, "m2")){ a_o = 3; t = 1; } else if(!strcmp(mode, "g8")){ a_o = 2; t = 7; } else if(!strcmp(mode, "g14")){ a_o = 2; t = 9; } else if(!strcmp(mode, "g12")){ a_o = 2; t = 9; } else if(!strcmp(mode, "aabbcc")){ a_o = 2; t = 3; } else if(!strcmp(mode, "f3")){ a_o = 3; t = 1; } else if(!strcmp(mode, "g11")){ a_o = 2; t = 5; } else if(!strcmp(mode, "g3")){ a_o = 2; t = 3; } else if(!strcmp(mode, "aabbcabba")){ a_o = 2; t = 5; } else usage(); load(); check(); display(); }