/*
燒雞
燒雞
燒雞 好想進選訓
燒雞
燒雞
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1005;
const ll MOD=(1LL<<61)-1;
char S[2*mxn][2*mxn];
ll hashv[2*mxn][2*mxn];
ll powx[2*mxn],powy[2*mxn];
random_device rng;
mt19937_64 seed(rng());
//ll X=abs((ll)seed());
//ll Y=abs((ll)seed());
ll X=3;
ll Y=4;
void genhash(int n,int m){
powx[0]=powy[0]=1;
for(int i=1;i<2*mxn;i++){
powx[i]=((__int128)powx[i-1]*X)%MOD;
}
for(int i=1;i<2*mxn;i++){
powy[i]=((__int128)powy[i-1]*Y)%MOD;
}
for(int i=1;i<=2*n;i++){
for(int j=1;j<=2*m;j++){
hashv[i][j]=(((__int128)hashv[i][j-1]*X)%MOD+S[i][j])%MOD;
}
}
for(int j=1;j<=2*m;j++){
for(int i=1;i<=2*n;i++){
hashv[i][j]=(((__int128)hashv[i-1][j]*Y)%MOD+hashv[i][j])%MOD;
}
}
}
ll cal(int a,int b,int xx,int yy){
int x=a+xx-1;
int y=b+yy-1;
ll ans=(hashv[x][y]-((__int128)hashv[x][b-1]*powx[y-b+1])%MOD)%MOD;
ans=(ans-((__int128)hashv[a-1][y]*powy[x-a+1])%MOD)%MOD;
ans=(ans+((__int128)(((__int128)hashv[a-1][b-1]*powy[x-a+1])%MOD)*powx[y-b+1])%MOD)%MOD;
return (ans+MOD)%MOD;
}
int main() {_
//setIO("wayne");
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>S[i][j];
S[i+n][j]=S[i][j+m]=S[i+n][j+m]=S[i][j];
}
}
genhash(n,m);
int ansx=1,ansy=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int l=i-1,r=i+n-1;
while(l<r){
int mid=(l+r+1)/2;
if(cal(i,j,mid-i+1,m)==cal(ansx,ansy,mid-i+1,m)){
l=mid;
}
else{
r=mid-1;
}
}
int row=l+1;
l=j-1;
r=j+m-1;
while(l<r){
int mid=(l+r+1)/2;
if(cal(row,j,1,mid-j+1)==cal(ansx+(row-i),ansy,1,mid-j+1)){
l=mid;
}
else{
r=mid-1;
}
}
int col=l+1;
if(row<=i+n-1 and col<=j+m-1){
if(S[row][col]<S[ansx+(row-i)][ansy+(col-j)]){
ansx=i,ansy=j;
}
}
}
}
for(int i=ansx;i<=ansx+n-1;i++){
for(int j=ansy;j<=ansy+m-1;j++){
cout<<S[i][j];
}
cout<<'\n';
}
return 0;
}
//maybe its multiset not set
Compilation message
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
980 KB |
Output is correct |
2 |
Correct |
3 ms |
988 KB |
Output is correct |
3 |
Correct |
3 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
984 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
3 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
980 KB |
Output is correct |
2 |
Correct |
3 ms |
988 KB |
Output is correct |
3 |
Correct |
3 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
984 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
3 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
988 KB |
Output is correct |
8 |
Correct |
175 ms |
6548 KB |
Output is correct |
9 |
Correct |
5 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
3924 KB |
Output is correct |
11 |
Correct |
182 ms |
6552 KB |
Output is correct |
12 |
Correct |
206 ms |
6676 KB |
Output is correct |
13 |
Correct |
202 ms |
6772 KB |
Output is correct |
14 |
Correct |
221 ms |
6772 KB |
Output is correct |
15 |
Correct |
180 ms |
6836 KB |
Output is correct |
16 |
Correct |
212 ms |
6772 KB |
Output is correct |
17 |
Correct |
229 ms |
6768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
980 KB |
Output is correct |
2 |
Correct |
3 ms |
988 KB |
Output is correct |
3 |
Correct |
3 ms |
980 KB |
Output is correct |
4 |
Correct |
4 ms |
984 KB |
Output is correct |
5 |
Correct |
3 ms |
980 KB |
Output is correct |
6 |
Correct |
3 ms |
980 KB |
Output is correct |
7 |
Correct |
4 ms |
988 KB |
Output is correct |
8 |
Correct |
175 ms |
6548 KB |
Output is correct |
9 |
Correct |
5 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
3924 KB |
Output is correct |
11 |
Correct |
182 ms |
6552 KB |
Output is correct |
12 |
Correct |
206 ms |
6676 KB |
Output is correct |
13 |
Correct |
202 ms |
6772 KB |
Output is correct |
14 |
Correct |
221 ms |
6772 KB |
Output is correct |
15 |
Correct |
180 ms |
6836 KB |
Output is correct |
16 |
Correct |
212 ms |
6772 KB |
Output is correct |
17 |
Correct |
229 ms |
6768 KB |
Output is correct |
18 |
Correct |
2354 ms |
36612 KB |
Output is correct |
19 |
Correct |
21 ms |
12628 KB |
Output is correct |
20 |
Correct |
35 ms |
1044 KB |
Output is correct |
21 |
Correct |
2381 ms |
36648 KB |
Output is correct |
22 |
Correct |
2844 ms |
36648 KB |
Output is correct |
23 |
Correct |
2303 ms |
36652 KB |
Output is correct |
24 |
Correct |
2877 ms |
36648 KB |
Output is correct |
25 |
Correct |
2313 ms |
36716 KB |
Output is correct |
26 |
Correct |
2693 ms |
36648 KB |
Output is correct |
27 |
Correct |
2858 ms |
36684 KB |
Output is correct |