/** * @brief Codeforces Round #283 (div. 2) c * @file c.cpp * @author mianma * @created 2014/12/22 13:38 * @edited 2014/12/22 13:38 * @type greedy * @time cost time o (M *n) * @mem cost mem 2*MAXN^2 + 2*MAXN * @note */ #include <fstream> #include <iostream> #include <cstring> #include < vector>using namespace std; #define &NBSP;MAX (a, b) ((a) > (b) ? (a) : (b)) #define &NBSP;MIN (A, b) ((a) > (b) ? (b) : (a)) #define &NBSP;ABS (a) ((a) > 0 (a) : (0 - (a))) #define &NBSP;CLR (VEC) memset (vec, 0, sizeof (VEC)) #ifdef debugifstream in;ofstream out; #define cin in#define cout out#else#define cin cin#define COUT cout#endif#define MAXN 110char table[MAXN][MAXN]; /*store input string*/int record[maxn][maxn]; /* record row status */int status1[maxn]; /*curr row ' s substring is the same with the prev row* /int status2[maxn]; int n, m;int ans = 0;int *curr = status1;int *old = status2;int main (void) {#ifdef debug freopen ("./in", "R", stdin) freopen ("./out", "W", stdout); #endif scanf ("%d %d ", &n, &m); for (int i = 0; i < n; i++) &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", table[i]); for (int i = 1; i < n; i++) for (int j = 0; j < m; j++) record[i][j] = table[i][j] - table[i - 1][j]; for (int col = 0; col < m; col++) { int fail = 0; for (int row = 1; row < n; row++) { /*substring is the same with the prev row*/ if (0 == old[row]) { if (record[row][col] > 0) curr[row] = 1; else if (record[row][col] < 0) { fail = 1; break; }else curr[row] = 0; }else curr[row] = 1; } if (!fail) {#ifdef DEBUG printf ("col %d insert\n", col); printf ("bmp new:\n"); for (int j = 1; j < n; j++) printf ("%d ", curr[j]); printf (" \ n "); #endif int * tmp = curr; curr = old; old = tmp; ++ans; } } printf ("%d\n ", m - ans); return 0;}
Codeforces Round #283 (Div. 2) C