Milking Grid
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 8062 | Accepted: 3493 |
Description
Every morning when they are milked, the Farmer John’s cows form a rectangular grid that is R (1 <= R <= 10,000) rows by C (1 <= C <= 75) columns. As we all know, Farmer John is quite the expert on cow behavior, and is currently writing a book about feeding behavior in cows. He notices that if each cow is labeled with an uppercase letter indicating its breed, the two-dimensional pattern formed by his cows during milking sometimes seems to be made from smaller repeating rectangular patterns.
Help FJ find the rectangular unit of smallest area that can be repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid, as indicated in the sample input below.
Help FJ find the rectangular unit of smallest area that can be repetitively tiled to make up the entire milking grid. Note that the dimensions of the small rectangular unit do not necessarily need to divide evenly the dimensions of the entire milking grid, as indicated in the sample input below.
Input
* Line 1: Two space-separated integers: R and C
* Lines 2..R+1: The grid that the cows form, with an uppercase letter denoting each cow’s breed. Each of the R input lines has C characters with no space or other intervening character.
* Lines 2..R+1: The grid that the cows form, with an uppercase letter denoting each cow’s breed. Each of the R input lines has C characters with no space or other intervening character.
Output
* Line 1: The area of the smallest unit from which the grid is formed
Sample Input
2 5 ABABA ABABA
Sample Output
2
Hint
The entire milking grid can be constructed from repetitions of the pattern ‘AB’.
题解:
题意是,给你一个矩阵,问你最小能用多大的矩阵来无限扩展成当前矩阵,可以超出范围。
那我们怎么做呢???
首先我们仔细观察,其最小子矩阵一定从左上角开始的,所以我们就可以找每一行的最小循环节,然后求一个最小公倍数,其一定是对于行来说是正确的;然后同理,对列也求一遍,找到最小循环节,然后求最小公倍数,然后两数相乘,一定是答案。
注意,当求的长度超过矩阵范围时,要是其等于矩阵的范围,因为任何一个矩阵都可以是其自身扩展0倍所得到的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a[10005][105];
int fail_h[10005][105];
int fail_l[105][10500];
int gcd(int a, int b)
{
if (a < b)
return gcd(b, a);
if (b == 0)
return a;
return gcd(b, a%b);
}
int lcm(int a, int b)
{
return a / gcd(a, b) * b;
}
void init_h(int l, int m)
{
fail_h[l][1] = 0;
for (int i = 2; i <= m; i++)
{
int p = fail_h[l][i-1];
while (p && a[l][i] != a[l][p+1])
p = fail_h[l][p];
if (a[l][i] == a[l][p+1])
p++;
fail_h[l][i] = p;
}
}
void init_l(int l, int m)
{
fail_l[l][1] = 0;
for (int i = 2; i <= m; i++)
{
int p = fail_l[l][i-1];
while (p && a[i][l] != a[p+1][l])
p = fail_l[l][p];
if (a[i][l] == a[p+1][l])
p++;
fail_l[l][i] = p;
}
}
int main()
{
int n, m, l = 1, r = 1;
cin >> n >> m;
for (int i = 1; i <= n; i++)
scanf("%s", a[i]+1);
for (int i = 1; i <= n; i++)
{
init_h(i, m);
l = lcm(l, m - fail_h[i][m]);
if (l >= m)
{
l = m;
break;
}
}
for (int i = 1; i <= m; i++)
{
init_l(i, n);
r = lcm(r, n - fail_l[i][n]);
if (r >= n)
{
r = n;
break;
}
}
cout << l * r;
return 0;
}