/* chargingChaos */ #include #include #include #include #define INF 99999999 #define MAX_N 100000 // Numero massimo di stringhe #define MAX_L 1000 // Lunghezza massima della stringa using namespace std; bool switcher[MAX_N+1]; char source[MAX_N+1][MAX_L]; struct A{ char p[MAX_L]; bool operator<(const A &q)const{ return strcmp(p, q.p)<0; } }target[MAX_N+1], r[MAX_N+1]; int main(){ //Apertura dello stream di input ifstream infile("input.txt"); assert(infile); int n, m, k, l, R, S; // Leggo le stringhe source infile >> n >> m; // Controllo che le variabili rispettino le assunzioni del problema assert(n < MAX_N); assert(m <= MAX_L); // ROMEO: assert(m < MAX_L); for(int j = 0; j < n; j++){ infile >> source[j]; } // leggo le stringhe target for(int j = 0; j < n; j++){ infile >> target[j].p; } // Chiusura dello stream di input infile.close(); //Ordino le parole target in ordine crescente sort(target, target+n); R = INF; for(int j = 0; j < n; j++){ S = 0; for(k = 0; k < m; k++){ if(source[0][k] != target[j].p[k]){ switcher[k] = 1; S++; } else { switcher[k] = 0; } } if(S >= R) continue; for(k = 0; k <= n; k++){ for(l = 0; l < m; l++){ r[k].p[l] = source[k][l]; if(switcher[l]){ r[k].p[l] = '0' + '1' - r[k].p[l]; } } r[k].p[m] = 0; } sort(r, r+n); for(k = 0; k < n; k++){ if(strcmp(r[k].p, target[k].p)!= 0) break; } if(k == n) R = S; } // Apro lo stream di output e scrivo i risultati ofstream outfile("output.txt"); if(R != INF){ outfile << R << endl; } else { outfile << "NOT POSSIBLE" << endl; } // Chiudo lo stream di output outfile.close(); return 0; }