Skip to content

Instantly share code, notes, and snippets.

@hikariyo
Created December 1, 2025 10:00
Show Gist options
  • Select an option

  • Save hikariyo/08f283fdc45479ef31b2c939c96b74fc to your computer and use it in GitHub Desktop.

Select an option

Save hikariyo/08f283fdc45479ef31b2c939c96b74fc to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
vector<int> buildnxt(string& s, int n) {
vector<int> nxt(n+1);
nxt[1] = 0;
for (int i = 2; i <= n; i++) {
nxt[i] = nxt[i-1];
while (nxt[i] && s[nxt[i] + 1] != s[i]) nxt[i] = nxt[nxt[i]];
nxt[i] += s[nxt[i] + 1] == s[i];
}
return nxt;
}
int solve(vector<string>& A, int n, int m) {
vector<int> nxt, cnt(m+1);
for (int i = 1; i <= n; i++) {
string& s = A[i];
nxt = buildnxt(s, m);
for (int j = nxt[m]; ; j = nxt[j]) {
cnt[m - j]++;
if (!j) break;
}
}
for (int i = 1; i <= m; i++) if (cnt[i] == n) return i;
assert(false);
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int R, C;
cin >> R >> C;
vector<string> A(R+1), AT(C+1);
for (int i = 1; i <= R; i++) {
cin >> A[i];
A[i] = ' ' + A[i];
}
for (int i = 1; i <= C; i++) {
AT[i] = " ";
for (int j = 1; j <= R; j++) {
AT[i] += A[j][i];
}
}
cout << solve(A, R, C) * solve(AT, C, R) << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment