#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
const int Nmax=2010;
const ull P=998244353, Q=1e11+19;
int N, M;
bool A[Nmax][Nmax];
ull Pm[Nmax], Qm[Nmax], Pi, Qi, Pim[Nmax], Qim[Nmax], H[Nmax][Nmax];
pair<int, int> X[Nmax*Nmax];
ull Pow(ull a, ull b) {
if(b==0) return 1;
ull tmp=Pow(a, b/2);
tmp*=tmp;
if(b%2) tmp*=a;
return tmp;
}
ull Hash2D(int sy, int sx, int ey, int ex) {
ull res=H[ey][ex]-H[sy-1][ex]-H[ey][sx-1]+H[sy-1][sx-1];
return res*Pim[sx-1]*Qim[sy-1];
}
ull Hash1D(int y, int sx, int ex) {
return Hash2D(y, sx, y, ex);
}
bool f(int ay, int ax, int by, int bx) {
int h=0, w=0;
for(int s=1, e=N-1; s<=e;) {
int m=(s+e)/2;
if(Hash2D(ay, ax, ay+m-1, ax+M-1)==Hash2D(by, bx, by+m-1, bx+M-1)) h=m, s=m+1;
else e=m-1;
}
for(int s=1, e=M; s<=e;) {
int m=(s+e)/2;
if(Hash1D(ay+h, ax, ax+m-1)==Hash1D(by+h, bx, bx+m-1)) w=m, s=m+1;
else e=m-1;
}
if(h==N-1 && w==M) return false;
if(w==M) return A[ay+h][ax]<A[by+h][bx];
return A[ay+h][ax+w]<A[by+h][bx+w];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>M;
for(int i=1; i<=N; i++) {
string S; cin>>S;
for(int j=1; j<=M; j++) A[i][j]=(S[j-1]=='.');
}
for(int i=1; i<=N; i++) {
for(int j=M+1; j<=2*M; j++) A[i][j]=A[i][j-M];
}
for(int i=N+1; i<=2*N; i++) {
for(int j=1; j<=2*M; j++) A[i][j]=A[i-N][j];
}
Pm[0]=Qm[0]=1;
for(int i=1; i<=2*max(N, M); i++) {
Pm[i]=Pm[i-1]*P;
Qm[i]=Qm[i-1]*Q;
}
Pi=Pow(P, ULLONG_MAX); Qi=Pow(Q, ULLONG_MAX);
Pim[0]=Qim[0]=1;
for(int i=1; i<=2*max(N, M); i++) {
Pim[i]=Pim[i-1]*Pi;
Qim[i]=Qim[i-1]*Qi;
}
for(int i=1; i<=2*N; i++) {
for(int j=1; j<=2*M; j++) {
H[i][j]=H[i][j-1]+H[i-1][j]-H[i-1][j-1]+A[i][j]*Pm[j-1]*Qm[i-1];
}
}
int x=1, y=1;
for(int i=1; i<=N; i++) {
for(int j=1; j<=M; j++) {
if(f(i, j, x, y)) x=i, y=j;
}
}
for(int i=x; i<=x+N-1; i++) {
for(int j=y; j<=y+M-1; j++) {
if(A[i][j]) cout<<".";
else cout<<"*";
}
cout<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
17 ms |
6580 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
3924 KB |
Output is correct |
11 |
Correct |
17 ms |
6612 KB |
Output is correct |
12 |
Correct |
18 ms |
6708 KB |
Output is correct |
13 |
Correct |
18 ms |
6828 KB |
Output is correct |
14 |
Correct |
18 ms |
6832 KB |
Output is correct |
15 |
Correct |
15 ms |
6868 KB |
Output is correct |
16 |
Correct |
18 ms |
6796 KB |
Output is correct |
17 |
Correct |
20 ms |
6868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
17 ms |
6580 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
3924 KB |
Output is correct |
11 |
Correct |
17 ms |
6612 KB |
Output is correct |
12 |
Correct |
18 ms |
6708 KB |
Output is correct |
13 |
Correct |
18 ms |
6828 KB |
Output is correct |
14 |
Correct |
18 ms |
6832 KB |
Output is correct |
15 |
Correct |
15 ms |
6868 KB |
Output is correct |
16 |
Correct |
18 ms |
6796 KB |
Output is correct |
17 |
Correct |
20 ms |
6868 KB |
Output is correct |
18 |
Correct |
191 ms |
36688 KB |
Output is correct |
19 |
Correct |
8 ms |
12628 KB |
Output is correct |
20 |
Correct |
4 ms |
1108 KB |
Output is correct |
21 |
Correct |
197 ms |
37696 KB |
Output is correct |
22 |
Correct |
209 ms |
37688 KB |
Output is correct |
23 |
Correct |
188 ms |
37708 KB |
Output is correct |
24 |
Correct |
212 ms |
37816 KB |
Output is correct |
25 |
Correct |
178 ms |
37868 KB |
Output is correct |
26 |
Correct |
241 ms |
37772 KB |
Output is correct |
27 |
Correct |
204 ms |
37652 KB |
Output is correct |