QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#107355 | #6105. Double-Colored Papers | bulijiojiodibuliduo# | WA | 31ms | 195024kb | C++ | 4.1kb | 2023-05-21 03:23:12 | 2023-05-21 03:23:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=201000;
const int M=26;
struct node {
node *go[27],*p;
node *ngo[27];
vector<node*> son;
int val,plen;
ll sub,rsub;
};
struct sam {
node* newnode() {
node *p=cur++;
p->val=p->plen=0; p->p=0;
memset(p->go,0,sizeof(p->go));
p->son.clear();
return p;
}
node *append(node *p,int w) {
node *np=newnode();np->val=p->val+1;
for (;p&&!p->go[w];p=p->p) p->go[w]=np;
if (!p) np->p=rt;
else {
node *q=p->go[w];
if (q->val==p->val+1) np->p=q;
else {
node *nq=newnode();
nq->val=p->val+1;
memcpy(nq->go,q->go,sizeof(q->go));
nq->p=q->p;
np->p=q->p=nq;
for (;p&&p->go[w]==q;p=p->p) p->go[w]=nq;
}
}
return np;
}
ll countdag(node *p) {
if (p->sub!=0) return p->sub;
p->sub=1;
rep(i,0,M) {
if (p->go[i]) p->ngo[i]=p;
else {
if (p->p) p->ngo[i]=p->p->ngo[i];
else p->ngo[i]=NULL;
}
}
rep(i,0,M) if (p->go[i]) p->sub+=countdag(p->go[i]);
return p->sub;
}
void dfs(node *p,int sl) {
int sr=p->val;
p->plen=sl;
rep(i,0,SZ(p->son)) {
p->son[i]->rsub=p->rsub+(sr-sl)*p->sub;
dfs(p->son[i],sr);
}
}
void init() {
n=strlen(s+1);
cur=pool;
rt=newnode();
node *np=rt;
rep(i,1,n+1) {
np=append(np,s[i]-'a');
}
for (node *p=pool;p!=cur;p++) if (p->p) p->p->son.pb(p);
countdag(rt);
dfs(rt,-1);
}
ll eval(node *p,int len) {
return p->rsub+(len-p->plen)*p->sub;
}
void nextgo(node* &p,int w,int &len) {
if (p->go[w]) p=p->go[w],++len;
else {
if (p->ngo[w]==NULL) p=rt,len=0;
else {
p=p->ngo[w]; len=p->val;
p=p->go[w]; ++len;
}
}
}
node pool[N],*cur,*rt;
char s[N];
int n;
}s,t;
ll k;
struct state {
node *s,*t,*t2;
int slen,lent,lent2,totlen;
};
ll eval(state ps) {
ll way=0;
if (ps.totlen==-1) return 0;
if (ps.s!=NULL) {
way+=(ps.s->sub-(ps.s==s.rt))*(t.rt->sub-1);
way+=t.eval(ps.t,ps.lent)-t.rt->sub;
if (ps.lent==ps.totlen&&ps.totlen!=0) way-=ps.t->sub;
return way;
} else {
if (ps.slen==0) return 0;
if (ps.lent<ps.totlen-ps.slen-1) return 0;
way=t.eval(ps.t,ps.lent)-t.eval(ps.t2,ps.totlen-ps.slen-1);
if (ps.lent==ps.totlen) way-=ps.t->sub;
return way;
}
}
state trans(state ps,int w) {
if (ps.totlen==-1) return ps;
++ps.totlen;
if (ps.s==NULL) {
t.nextgo(ps.t,w,ps.lent);
if (ps.lent+ps.slen<ps.totlen) {
ps.totlen=-1;
return ps;
}
assert(ps.t2->go[w]!=NULL);
ps.t2=ps.t2->go[w];
} else {
if (ps.s->go[w]) {
ps.s=ps.s->go[w];
t.nextgo(ps.t,w,ps.lent);
} else {
ps.s=NULL;
ps.slen=ps.totlen-1;
t.nextgo(ps.t,w,ps.lent);
if (ps.lent+ps.slen<ps.totlen) {
ps.totlen=-1;
return ps;
}
ps.t2=t.rt;
}
}
return ps;
}
state ps;
ll cway[30];
int main() {
scanf("%s%s",s.s+1,t.s+1);
s.init(); t.init();
scanf("%lld",&k);
if (k>(s.rt->sub-1)*(t.rt->sub-1)) {
puts("-1");
}
ps.s=s.rt; ps.t=t.rt;
string ans;
/*auto x=trans(ps,1);
x=trans(x,1);
printf("!! %lld\n",eval(x));*/
while (1) {
ll way=eval(ps);
//puts("gao");
//puts(ans.c_str());
rep(i,0,M) {
auto qs=trans(ps,i);
cway[i]=eval(qs);
//printf("--- %d %d\n",i,cway[i]);
way-=cway[i];
}
if (way>=k) {
break;
}
k-=way;
rep(i,0,M) {
if (cway[i]>=k) {
ps=trans(ps,i);
ans.pb(i+'a');
break;
} else {
k-=cway[i];
}
}
}
puts(ans.c_str());
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 31ms
memory: 194968kb
input:
tww wtw 21
output:
wwtw
result:
ok single line: 'wwtw'
Test #2:
score: -100
Wrong Answer
time: 31ms
memory: 195024kb
input:
abbadacbad deebbaecdc 164
output:
abbadacbae
result:
wrong answer 1st lines differ - expected: 'abbadacbaecd', found: 'abbadacbae'