#include #include #include class Vector { public: Vector* next; char* representation; char* name; }; Vector* all_vectors; int nr_vec; int nr_pos; long counts[1000]; void read(FILE *f) { char buf[1000]; nr_vec = 0; nr_pos = 0; all_vectors = 0; Vector** ref_vector = &all_vectors; while (fgets(buf, 1000, f)) { if (nr_pos == 0) { for (int i = 0; i < 1000 && (buf[i] == '0' || buf[i] == '1'); i++) nr_pos++; for (int i = 0; i < nr_pos; i++) { counts[i] = 0; } } nr_vec++; Vector *vector = new Vector; *ref_vector = vector; ref_vector = &vector->next; vector->representation = (char*)malloc(sizeof(char)*(nr_pos+1)); strncpy(vector->representation, buf, nr_pos); vector->representation[nr_pos] = '\0'; char *s = buf+nr_pos; while (*s == ' ') s++; int l = strlen(s); while (s[l-1] < ' ') s[--l] = '\0'; vector->name = (char*)malloc(sizeof(char)*(l+1)); strcpy(vector->name, s); } } int main(int argc, char* argv[]) { read(stdin); printf("%d %d\n", nr_vec, nr_pos); for (int i = 0; i < nr_pos; i++) counts[i] = 0; for (Vector* vector = all_vectors; vector != 0; vector = vector->next) { for (int i = 0; i < nr_pos; i++) if (vector->representation[i] == '1') counts[i]++; } for (bool progress = true; progress;) { progress = false; for (int i = 0; i < nr_pos; i++) for (int j = 0; j < nr_pos; j++) if (counts[i] < counts[j]) { if (counts[i] == 0) { printf("Unsolvable\n"); return 0; } bool implied = true; for (Vector* vector = all_vectors; vector != 0; vector = vector->next) { if (vector->representation[i] == '1' && vector->representation[j] == '0') { implied = false; break; } } if (implied) { printf("Column %d implies column %d. Reduced number vectors with %d", i+1, j+1, counts[j]-counts[i]); for (Vector** ref_vector = &all_vectors; *ref_vector != 0; ) { Vector* vector = *ref_vector; if (vector->representation[i] == '0' && vector->representation[j] == '1') { nr_vec--; for (int k = 0; k < nr_pos; k++) if (vector->representation[k] == '1') counts[k]--; *ref_vector = vector->next; } else ref_vector = &vector->next; } printf(" to %d.\n", nr_vec); progress = true; } } } printf("\n\nLeft:\n"); for (Vector* vector = all_vectors; vector != 0; vector = vector->next) printf("%s %s\n", vector->representation, vector->name); return 0; }