QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#179591 | #7234. Pencil of Wishing | PhantomThreshold# | WA | 0ms | 28424kb | C++20 | 3.5kb | 2023-09-14 22:41:33 | 2023-09-14 22:41:33 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define lowbit(x) ((x)&(-x))
using namespace std;
const int maxn = 1400;
string S,T,str;
int sn,tn,n;
int gos[maxn][maxn],gose[maxn][maxn],got[maxn][maxn];
int mnlen[maxn];
struct SAM
{
int las,rt,tot;
int par[maxn],len[maxn],ri[maxn];
int son[maxn][28];
int newnode(int L)
{
++tot;
memset(son[tot],0,sizeof son[tot]);
par[tot]=0;
len[tot]=L;
return tot;
}
void init()
{
tot=0;
las=rt=newnode(0);
}
void extend(const int w,const int r)
{
int p=las;
int np=newnode(len[p]+1); ri[np]=r;
while(p && !son[p][w]) son[p][w]=np,p=par[p];
if(!p) par[np]=rt;
else
{
int q=son[p][w];
if(len[q]==len[p]+1) par[np]=q;
else
{
int nq=newnode(len[p]+1);
memcpy(son[nq],son[q],sizeof son[nq]);
par[nq]=par[q];
par[np]=par[q]=nq;
while(p&&son[p][w]==q) son[p][w]=nq,p=par[p];
}
}
las=np;
}
vector<int>V[maxn];
set<int>S[maxn];
void dfs(const int x)
{
if(ri[x]) S[x].insert(ri[x]);
for(auto y:V[x]) dfs(y);
for(auto y:V[x])
{
if(S[x].size()<S[y].size()) swap(S[x],S[y]);
for(auto it:S[y])
S[x].insert(it);
}
for(int i=0;i<=sn;i++)
{
auto it= S[x].lower_bound(i+mnlen[x]);
if(it==S[x].end() || *it>sn) gos[i][x]= -1;
else gos[i][x]= *it;
if(i+mnlen[x]<=sn)
{
it= S[x].find(sn);
if(it!=S[x].end()) gose[i][x]=1;
}
}
for(int i=0;i<=tn;i++)
{
auto it=S[x].lower_bound(sn+1+i+mnlen[x]);
if(it==S[x].end()) got[i][x]= -1;
else got[i][x]= (*it)-sn-1;
}
}
void build()
{
for(int i=2;i<=tot;i++)
{
V[par[i]].push_back(i);
mnlen[i]= len[par[i]]+1;
}
dfs(rt);
}
}tr;
int f[maxn][maxn];
tuple<int,int,int>g[maxn][maxn];
signed main()
{
ios_base::sync_with_stdio(false); ////////////////////////////////////////
cin.tie(0);
cin>>S; sn=S.size();
cin>>T; tn=T.size();
str= S+(char)('z'+1)+T; n=str.size();
tr.init();
for(int i=0;i<n;i++)
tr.extend(str[i]-'a',i+1);
tr.build();
memset(f,-1,sizeof f);
f[0][0]=0;
for(int i=0;i<=sn;i++) for(int j=0;j<=tn;j++) if(f[i][j]!=-1)
{
for(int x=2;x<=tr.tot;x++) if(gos[i][x]!=-1)
{
int nexi=gos[i][x];
int nexj= got[j][x]==-1?tn+1:got[j][x];
if(i+mnlen[x]==nexi && j+mnlen[x]!=nexj) nexj=tn+1;
int temp= f[i][j]+mnlen[x]+ (i+mnlen[x]!=nexi);
if( f[nexi][nexj]==-1 || f[nexi][nexj]>temp )
{
f[nexi][nexj]=temp;
g[nexi][nexj]= make_tuple( i,j,mnlen[x] );
}
if(gose[i][x])
{
nexi=sn;
temp= f[i][j]+ sn-i + 0;
if( f[nexi][nexj]==-1 || f[nexi][nexj]>temp )
{
f[nexi][nexj]=temp;
g[nexi][nexj]= make_tuple( i,j,mnlen[x] );
}
}
}
}
int ansi,ansj,ans=sn+1;
for(int i=1;i<=sn;i++) if(f[i][tn+1]!=-1)
{
int j=tn+1;
int tc= f[i][j]+ (i!=sn);
if( tc<ans)
{
ans=tc;
ansi=i;
ansj=j;
}
}
for(int j=0;j<=tn+1;j++) if(f[sn][j]!=-1 && f[sn][j]==sn)
{
int i=sn;
int tc=f[i][j];
if( tc<ans)
{
ans=tc;
ansi=i;
ansj=j;
}
}
int i=ansi,j=ansj;
vector<string>outp;
if(i!=sn) outp.emplace_back("*");
while(i!=0)
{
auto [pi,pj,len]= g[i][j];
outp.push_back( S.substr(i-len+1-1,len) );
if(pi+len!=i) outp.emplace_back("*");
i=pi,j=pj;
}
//cerr<<ans<<endl;
for(int k=(int)outp.size()-1;k>=0;k--) cout<<outp[k];
cout<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 26120kb
input:
aabb ab
output:
aa*
result:
ok correct
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 28424kb
input:
abaabaaabbbaabbb abaabbbaabaaabbb
output:
*aaba*
result:
wrong answer pattern matches the second string