QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#938#374106#7686. The Phantom Menaceship2077arnold518Success!2024-10-07 19:50:402024-10-07 19:50:40

Details

Extra Test:

Wrong Answer
time: 37ms
memory: 365004kb

input:

1
1 50
icaakmaacrabgaaaaaaaaaaaaaaaaaaaaaaabaacaaeyaahkaa
aakhaayeaacaabaaaaaaaaaaaaaaaaaaaaaaagbarcaamkaaci

output:

1 
1 

result:

wrong answer not cyclic isomorphism (test case 1)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#374106#7686. The Phantom Menacearnold518WA 525ms671232kbC++174.8kb2024-04-02 11:56:222024-10-13 19:49:16

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 2e6;

const int MOD = 1e9+7;
struct mint
{
    int x;
    mint() : x(0) {}
    mint(int _x) : x(_x) {}
    mint operator + (int ot) const { return x+ot>=MOD ? x+ot-MOD : x+ot; }
    mint operator - () const { return x ? MOD-x : 0; }
    mint operator - (int ot) const { return x<ot ? x+MOD-ot : x-ot; }
    mint operator * (int ot) const { return 1ll*x*ot%MOD; }
    mint operator += (int ot) { return *this = *this + ot; }
    mint operator -= (int ot) { return *this = *this - ot; }
    mint operator *= (int ot) { return *this = *this * ot; }
    operator int() const { return x; }
    bool operator < (int ot) const { return x<ot; }
    bool operator == (int ot) const { return x==ot; }
};
const mint P1 = 72;
const mint P2 = 41;

typedef pair<mint, mint> pmm;

int TC, N, M;
string A[MAXN+10], B[MAXN+10];
vector<pmm> PA[MAXN+10], QA[MAXN+10];
vector<pmm> PB[MAXN+10], QB[MAXN+10];

vector<pii> adj[MAXN+10];
vector<int> ans;
int deg[MAXN+10];

void dfs(int now)
{
    while(!adj[now].empty())
    {
        pii nxt=adj[now].back();
        adj[now].pop_back();
        dfs(nxt.first);
        ans.push_back(nxt.second);
    }
}

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

    cin>>TC;
    while(TC--)
    {
        cin>>N>>M;
        for(int i=1; i<=N; i++) cin>>A[i], A[i]="?"+A[i];
        for(int i=1; i<=N; i++) cin>>B[i], B[i]="?"+B[i];

        for(int i=1; i<=N; i++)
        {
            PA[i]=vector<pmm>(M+2);
            QA[i]=vector<pmm>(M+2);

            mint val1=1, val2=1;
            for(int j=1; j<=M; j++)
            {
                val1*=P1; val2*=P2;
                PA[i][j]={PA[i][j-1].first+val1*(A[i][j]-'a'+1), PA[i][j-1].second+val2*(A[i][j]-'a'+1)};
            }
            for(int j=M; j>=1; j--)
            {
                QA[i][j]={(QA[i][j+1].first+(A[i][j]-'a'+1))*P1, (QA[i][j+1].second+(A[i][j]-'a'+1))*P2};
            }
        }
        for(int i=1; i<=N; i++)
        {
            PB[i]=vector<pmm>(M+2);
            QB[i]=vector<pmm>(M+2);

            mint val1=1, val2=1;
            for(int j=1; j<=M; j++)
            {
                val1*=P1; val2*=P2;
                PB[i][j]={PB[i][j-1].first+val1*(B[i][j]-'a'+1), PB[i][j-1].second+val2*(B[i][j]-'a'+1)};
            }
            for(int j=M; j>=1; j--)
            {
                QB[i][j]={(QB[i][j+1].first+(B[i][j]-'a'+1))*P1, (QB[i][j+1].second+(B[i][j]-'a'+1))*P2};
            }
        }

        bool flag2=false;
        for(int i=0; i<M; i++)
        {
            vector<pmm> VA, VB;
            for(int j=1; j<=N; j++)
            {
                VA.push_back(PA[j][i]);
                VB.push_back(QA[j][i+1]);
            }
            sort(VA.begin(), VA.end());
            sort(VB.begin(), VB.end());
            VA.erase(unique(VA.begin(), VA.end()), VA.end());
            VB.erase(unique(VB.begin(), VB.end()), VB.end());

            bool flag=true;
            for(int j=1; j<=N; j++)
            {
                if(!binary_search(VA.begin(), VA.end(), QB[j][M-i+1])) { flag=false; break; }
                if(!binary_search(VB.begin(), VB.end(), PB[j][M-i])) { flag=false; break; }
            }
            if(!flag) continue;

            for(int j=1; j<=N; j++)
            {
                int v=(lower_bound(VA.begin(), VA.end(), QB[j][M-i+1])-VA.begin())+1;
                int u=(lower_bound(VB.begin(), VB.end(), PB[j][M-i])-VB.begin())+VA.size()+1;
                adj[u].push_back({v, -j});
                deg[u]++; deg[v]--;
            }
            for(int j=1; j<=N; j++)
            {
                int u=(lower_bound(VA.begin(), VA.end(), PA[j][i])-VA.begin())+1;
                int v=(lower_bound(VB.begin(), VB.end(), QA[j][i+1])-VB.begin())+VA.size()+1;
                adj[u].push_back({v, j});
                deg[u]++; deg[v]--;
            }

            for(int j=1; j<=N+N; j++) if(deg[j]!=0) flag=false;
            for(int j=1; j<=N+N; j++) deg[j]=0;
            if(!flag)
            {
                for(int j=1; j<=N+N; j++) adj[j].clear();
                continue;
            }

            dfs(1);
            for(int j=1; j<=N+N; j++) adj[j].clear();
            if(ans.size()!=N+N)
            {
                ans.clear();
                continue;
            }

            flag2=true;
            reverse(ans.begin(), ans.end());
            for(auto it : ans) if(it>0) cout<<it<<" "; cout<<"\n";
            for(auto it : ans) if(it<0) cout<<-it<<" "; cout<<"\n";
            ans.clear();
            break;
        }
        if(!flag2) cout<<"-1\n";
    }
}