题目貌似有点难理解,忍不住看了一下解题报告。。。囧
方法:对每行求最小重复长度,求出lcm,记为ans1;再对每列求最小重复长度,求出lcm,记为ans2
ans1*ans2就是最后答案
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout)
#define N 1000005
char str[10005][80];
int x[80],y[10005];
int main () {
int r,c;
while (scanf("%d%d",&r,&c) != -1) {
int i,j,k;
for (i = 1; i <= r; i++) {
scanf("%s",str[i] + 1);
}
int xx = 1;
for (i = 1; i <= r; i++) {
x[1] = 0;
k = 0;
for (j = 2; j <= c; j++) {
while (k > 0 && str[i][k+1] != str[i][j]) {
k = x[k];
}
if (str[i][k+1] == str[i][j]) k++;
x[j] = k;
}
int m = c - x[c];cout<<x[c]<<endl;
xx = xx / abs(__gcd(xx,m)) * m;
}
int yy = 1;
for (i = 1; i <= c; i++) {
y[1] = 0;
k = 0;
for (j = 2; j <= r; j++) {
while (k > 0 && str[k+1][i] != str[j][i]) {
k = y[k];
}
if (str[k+1][i] == str[j][i]) k++;
y[j] = k;
}
int m = r - y[r];
yy = yy / abs(__gcd(yy,m)) * m;
}
if (xx > c) xx = c;
if (yy > r) yy = r;
printf("%d\n",xx * yy);
}
return 0;
}