QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#670424 | #7686. The Phantom Menace | crush_codemaker | WA | 132ms | 698160kb | C++14 | 4.7kb | 2024-10-23 21:39:46 | 2024-10-23 21:39:47 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define N 1000007
using namespace std;
int T,n,m;
char a[N],b[N];
int ah1[N],ah2[N];
int bh1[N],bh2[N];
const int b1=103;
const int p1=998244353;
const int b2=107;
const int p2=1e9+7;
int cf1[N],cf2[N];
map<pair<int,int>,int>pID;
map<pair<int,int>,int>eID;
stack<int>sk[N];
int pn,en;
int first[N<<2],nxt[N<<1],to[N<<1],tot;
int d[N<<2];
void add(int u,int v)
{
tot++;
to[tot]=v;
nxt[tot]=first[u];
first[u]=tot;
d[u]++;
d[v]--;
}
void pre()
{
ah1[0]=ah2[0]=0;
bh1[0]=bh2[0]=0;
for(int i=1;i<=n*m;i++)
{
int c=a[i]-'a';
ah1[i]=(c+(ah1[i-1]*b1)%p1)%p1;
ah2[i]=(c+(ah2[i-1]*b2)%p2)%p2;
c=b[i]-'a';
bh1[i]=(c+(bh1[i-1]*b1)%p1)%p1;
bh2[i]=(c+(bh2[i-1]*b2)%p2)%p2;
}
cf1[0]=cf2[0]=1;
for(int i=1;i<=m;i++)
{
cf1[i]=(cf1[i-1]*b1)%p1;
cf2[i]=(cf2[i-1]*b2)%p2;
}
}
pair<int,int>ah(int l,int r)
{
return {((ah1[r]-(ah1[l-1]*cf1[r-l+1])%p1)%p1+p1)%p1,((ah2[r]-(ah2[l-1]*cf2[r-l+1])%p2)%p2+p2)%p2};
}
pair<int,int>bh(int l,int r)
{
return {((bh1[r]-(bh1[l-1]*cf1[r-l+1])%p1)%p1+p1)%p1,((bh2[r]-(bh2[l-1]*cf2[r-l+1])%p2)%p2+p2)%p2};
}
pair<int,int>ap(int i,int x)
{
pair<int,int>ans=ah((i-1)*m+1,(i-1)*m+x);
ans.first=2*ans.first+1;
ans.second=2*ans.second+1;
return ans;
}
pair<int,int>as(int i,int x)
{
pair<int,int>ans=ah((i-1)*m+m-x+1,(i-1)*m+m);
ans.first=2*ans.first;
ans.second=2*ans.second;
return ans;
}
pair<int,int>bp(int i,int x)
{
pair<int,int>ans=bh((i-1)*m+1,(i-1)*m+x);
ans.first=2*ans.first;
ans.second=2*ans.second;
return ans;
}
pair<int,int>bs(int i,int x)
{
pair<int,int>ans=bh((i-1)*m+m-x+1,(i-1)*m+m);
ans.first=2*ans.first+1;
ans.second=2*ans.second+1;
return ans;
}
stack<int>path;
void dfs(int u)
{
for(int i=first[u];i;i=first[u])
{
first[u]=nxt[i];
int v=to[i];
dfs(v);
}
path.push(u);
}
int A[N],B[N];
int ta,tb;
int deal(int x)
{
for(int i=1;i<=4*n;i++)
{
first[i]=0;
d[i]=0;
}
tot=0;
for(int i=1;i<=2*n;i++)
{
while((int)sk[i].size())
{
sk[i].pop();
}
}
pID.clear();
eID.clear();
pn=0,en=0;
for(int i=1;i<=n;i++)
{
pair<int,int>fi=ap(i,x),se=as(i,m-x);
if(!pID[fi])
{
pID[fi]=++pn;
}
if(!pID[se])
{
pID[se]=++pn;
}
int fid=pID[fi],sid=pID[se];
if(!eID[{fid,sid}])
{
eID[{fid,sid}]=++en;
}
sk[eID[{fid,sid}]].push(2*i);
add(fid,sid);
}
for(int i=1;i<=n;i++)
{
pair<int,int>fi=bp(i,m-x),se=bs(i,x);
if(!pID[fi])
{
pID[fi]=++pn;
}
if(!pID[se])
{
pID[se]=++pn;
}
int fid=pID[fi],sid=pID[se];
if(!eID[{fid,sid}])
{
eID[{fid,sid}]=++en;
}
sk[eID[{fid,sid}]].push(2*i+1);
add(fid,sid);
}
for(int i=1;i<=pn;i++)
{
if(d[i]!=0)
{
return 0;
}
}
while((int)path.size())
{
path.pop();
}
dfs(1);
ta=0;tb=0;
int u=path.top();
path.pop();
while((int)path.size())
{
int v=path.top();
path.pop();
int e=eID[{u,v}];
int z=sk[e].top();
sk[e].pop();
if(z&1)
{
B[++tb]=z/2;
}
else
{
A[++ta]=z/2;
}
u=v;
}
if(ta!=n||tb!=n)
{
return 0;
}
return 1;
}
void solve()
{
scanf("%lld%lld",&n,&m);
getchar();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
a[(i-1)*m+j]=getchar();
}
getchar();
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
b[(i-1)*m+j]=getchar();
}
getchar();
}
pre();
for(int i=0;i<m;i++)
{
int jj=deal(i);
if(jj)
{
cout<<i<<endl;
for(int j=1;j<=n;j++)
{
printf("%lld ",A[j]);
}
printf("\n");
for(int j=1;j<=n;j++)
{
printf("%lld ",B[j]);
}
printf("\n");
return ;
}
}
printf("-1\n");
return ;
}
signed main()
{
cin>>T;
while(T--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 132ms
memory: 698160kb
input:
2 3 3 abc ghi def bcd efg hia 1 3 abc def
output:
1 1 3 2 1 2 3 -1
result:
wrong answer p is not permutation (test case 1)