POJ 2185 Milking Grid KMP cycle section cycle

Source: Internet
Author: User

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 &AMP;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.