Source of the topic:
id=2185 "target=" _blank ">poj 2185 milking Grid
Test instructions: At least how many large sub-matrices can cover the full map
For example, you can use an AB to make a
Ababab
Ababab can come out more.
Idea: Each line to find the period of a total of N to find the N-cycle of the least common multiple hypothesis is greater than M take M
Each column is calculated for a total of M-least common multiple of the M-cycle, assuming that the number is greater than n
The answer is the product of 2 least common multiple.
#include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib>using namespace std ; const int MAXN = 10010;char A[maxn][77];char B[77][MAXN]; int f[maxn][77];int f2[77][maxn];int gcd (int a, int b) {return b?gcd (b, a%b): A;} void Getfail (char* p, int* f) {int m = strlen (P); f[0] = f[1] = 0;for (int i = 1; i < m; i++) {int J = f[i];while (J &&A mp P[i] = p[j]) j = f[j];f[i+1] = p[i] = = P[j]? j+1:0;}} int main () {int n, m;scanf ("%d%d", &n, &m), for (int i = 1; I <= n; i++) {scanf ("%s", A[i]); Getfail (A[i], f[i]);} for (int i = 0, i < m; i++) {for (int j = 1; J <= N; j + +) {B[i+1][j-1] = a[j][i];} B[i+1][n] = 0;} for (int i = 1; I <= m; i++) Getfail (B[i], f2[i]), int ans1 = 1, ans2 = 1;for (int i = 1; I <= n; i++) {ans1 = ANS1/GCD (A NS1, M-f[i][m]) * (m-f[i][m]); if (Ans1 > m) {ans1 = M;break;}} for (int i = 1; I <= m; i++) {ans2 = ANS2/GCD (Ans2, N-f2[i][n]) * (N-f2[i][n]); if (Ans2 > N) {ans2 = N;break;}} printf ("%d\n", ans1*ans2); return 0;}
POJ 2185 Milking Grid KMP cycle section cycle