Submission #1357637

#TimeUsernameProblemLanguageResultExecution timeMemory
1357637liptonekSateliti (COCI20_satellti)C++20
110 / 110
207 ms37924 KiB
#include <bits/stdc++.h>
using namespace std;

const unsigned long long BASE1=131;
const unsigned long long BASE2=13331;

unsigned long long pp[1005];
unsigned long long qp[1005];
unsigned long long h[2005][2005];

int n,m;

string mat[1005];
string bit[2005];

unsigned long long hashing(int r, int c, int hr, int wc)
{
    unsigned long long res=h[r+hr][c+wc];

    res-=h[r][c+wc]*pp[hr];
    res-=h[r+hr][c]*qp[wc];
    res+=h[r][c]*pp[hr]*qp[wc];

    return res;
}

int compare_rows(int r1, int c1, int r2, int c2, int len)
{
    int low=1;
    int high=len;
    int ans=0;

    while(low<=high)
    {
        int mid=low+(high-low)/2;

        if(hashing(r1,c1,1,mid)==hashing(r2,c2,1,mid))
        {
            ans=mid;
            low=mid+1;
        }
        else
        {
            high=mid-1;
        }
    }

    if(ans==len)
    {
        return 0;
    }

    return (bit[r1][c1+ans]<bit[r2][c2+ans]) ? -1 : 1;
}

int compare_matrices(int r1, int c1, int r2, int c2)
{
    int low=1;
    int high=n;
    int ans=0;

    while(low<=high)
    {
        int mid=(low+high)/2;

        if(hashing(r1,c1,mid,m)==hashing(r2,c2,mid,m))
        {
            ans=mid;
            low=mid+1;
        }
        else
        {
            high=mid-1;
        }
    }

    if(ans==n)
    {
        return 0;
    }

    return compare_rows(r1+ans,c1,r2+ans,c2,m);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin>>n>>m;

    for(int i=0; i<n; i++)
    {
        cin>>mat[i];
    }

    for(int i=0; i<2*n; i++)
    {
        bit[i].reserve(2*m);

        for(int j=0; j<2*m; j++)
        {
            bit[i]+=mat[i%n][j%m];
        }
    }

    pp[0]=1;
    qp[0]=1;

    for(int i=1; i<=1000; i++)
    {
        pp[i]=pp[i-1]*BASE2;
        qp[i]=qp[i-1]*BASE1;
    }

    for(int i=0; i<2*n; i++)
    {
        unsigned long long row=0;

        for(int j=0; j<2*m; j++)
        {
            row=row*BASE1+(unsigned long long)bit[i][j];
            h[i+1][j+1]=h[i][j+1]*BASE2+row;
        }
    }

    int br=-1;
    int bc=-1;

    for(int c=0; c<m; c++)
    {
        int i=0;
        int j=1;
        int k=0;

        while(i<n && j<n && k<n)
        {
            int cmp=compare_rows(i+k,c,j+k,c,m);

            if(cmp==0)
            {
                k++;
            }
            else
            {
                if(cmp>0)
                {
                    i+=k+1;
                }
                else
                {
                    j+=k+1;
                }

                if(i==j)
                {
                    j++;
                }

                k=0;
            }
        }

        int rc=min(i,j);

        if(br==-1 || compare_matrices(rc,c,br,bc)<0)
        {
            br=rc;
            bc=c;
        }
    }

    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            cout<<bit[br+i][bc+j];
        }

        cout<<endl;
    }

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...