好久没看的
K
M
P
KMP
KMP
题解:
这个题一开始的思路是想看每一行的最短不整除循环节然后求一个
l
c
m
lcm
lcm,但是最后被自己的数据Hack了…
后来看了一下题解发现还可以直接对字符串数组进行
K
M
P
KMP
KMP,原理其实是一样的,不过可以更快得确定完全覆盖需要得最小行数和最小列数。
很妙的思路
#include <bits/stdc++.h>
#define ill __int128
#define ll long long
#define PII pair <ll,ll>
#define ull unsigned long long
#define me(a,b) memset (a,b,sizeof(a))
#define rep(i,a,b) for (int i = a;i <= b;i ++)
#define req(i,a,b) for (int i = a;i >= b;i --)
#define ios std :: ios :: sync_with_stdio(false)
const double Exp = 1e-9;
const int INF = 0x3f3f3f3f;
const int inf = -0x3f3f3f3f;
const ll mode = 1000000007;
const double pi = 3.141592653589793;
using namespace std;
const int maxn = 1e4 + 10;
int Next[maxn] = {}, R, C, len;
string r[maxn], l[maxn];
void calc_next1()
{
me (Next, 0);
Next[1] = 0;
for (int i = 2, j = 0;i <= R;i ++) {
while (j > 0 && r[i] != r[j + 1]) j = Next[j];
if (r[i] == r[j + 1]) j ++;
Next[i] = j;
}
return ;
}
void calc_next2()
{
me (Next, 0);
Next[1] = 0;
for (int i = 2, j = 0;i <= C;i ++) {
while (j > 0 && l[i] != l[j + 1]) j = Next[j];
if (l[i] == l[j + 1]) j ++;
Next[i] = j;
}
return ;
}
int main()
{
ios;
cin >> R >> C;
len = C;
for (int i = 1;i <= R;i ++) {
cin >> r[i];
r[i] = '$' + r[i];
}
calc_next1();
int ans1 = R - Next[R];
for (int i = 1;i <= C;i ++) {
l[i] += '$';
for (int j = 1;j <= ans1;j ++) {
l[i] += r[j][i];
}
}
calc_next2();
int ans2 = C - Next[C];
cout << ans1 * ans2 << endl;
return 0;
}