/* -*- coding: utf-8 -*- * * 1909.cc: No.1909 Detect from Substrings - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 1000; const int MAX_NM = 1000000; /* typedef */ /* global variables */ char buf[MAX_NM + 4], *ss[MAX_N]; char t0[MAX_NM + 4], t1[MAX_NM + 4]; /* subroutines */ bool issubstr(int m, char t[], char s[]) { int j = 0; for (int i = 0; i < m + 1 && j < m; i++) if (t[i] == s[j]) j++; return (j == m); } bool check(int n, int m, char t[]) { for (int i = 0; i < n; i++) if (! issubstr(m, t, ss[i])) return false; return true; } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { ss[i] = buf + m * i; scanf("%s", ss[i]); } //puts(buf); int k = 0; while (ss[0][k] == ss[1][k]) k++; //printf("k=%d\n", k); copy(ss[0], ss[0] + k + 1, t0); copy(ss[1] + k, ss[1] + m, t0 + k + 1); copy(ss[1], ss[1] + k + 1, t1); copy(ss[0] + k, ss[0] + m, t1 + k + 1); //printf("t0=%s, t1=%s\n", t0, t1); int c = 0; if (check(n, m, t0)) c++; if (check(n, m, t1)) c++; printf("%d\n", c); return 0; }