QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#603815 | #7039. Counting Failures on a Trie | tarjen | AC ✓ | 1028ms | 56388kb | C++20 | 4.9kb | 2024-10-01 19:32:35 | 2024-10-01 19:32:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const bool test=true;
const int maxn=1e5+10;
struct Trie{
int trie[maxn][26],tot;
int fa[maxn];
int dep[maxn],f[21][maxn];
void init(){
for(int i=0;i<=tot;i++){
memset(trie[i],0,sizeof(trie[i]));
fa[i]=0;
dep[i]=0;
for(int j=0;j<=20;j++)f[j][i]=0;
}
tot=0;
}
queue<int> qu;
int getkthfather(int x,int k){
for(int i=20;i>=0;i--)if(k>>i&1)x=f[i][x];
return x;
}
};
Trie tri;
const int N = 2*maxn;//2*strlen
struct Suffix{
int ht[N],rk[N],sa[N],y[N],c[N];
int n,m;
char s[N];
int st[20][N];
void init(){
n=strlen(s+1);
m=300;
for(int i=0;i<=m;i++) c[i]=0;
for(int i=0;i<=2*n;i++) y[i]=0;
for(int i=1;i<=n;i++) c[rk[i]=s[i]]++;
for(int i=1;i<=m;i++) c[i]+=c[i-1];
for(int i=n;i>=1;i--) sa[c[rk[i]]--]=i;
for(int k=1;k<=n;k<<=1){
int p=0;
for(int i=n-k+1;i<=n;i++) y[++p]=i;
for(int i=1;i<=n;i++){
if(sa[i]>k){
y[++p]=sa[i]-k;
}
}
for(int i=0;i<=m;i++) c[i]=0;
for(int i=1;i<=n;i++) c[rk[i]]++;
for(int i=1;i<=m;i++) c[i]+=c[i-1];
for(int i=n;i>=1;i--) sa[c[rk[y[i]]]--]=y[i];
for(int i=0;i<=n;i++) swap(rk[i],y[i]);
rk[sa[1]]=p=1;
for(int i=2;i<=n;i++){
rk[sa[i]]=(y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p : ++p);
}
if(p>=n) break;
m=p;
}
for(int i=1,k=0;i<=n;i++){
if(k)k--;
int j=sa[rk[i]-1];
while(s[i+k] == s[j+k] )k++;
ht[rk[i]] = k;
}
for(int i=1;i<=n;i++)st[0][i]=ht[i];
for(int j=1;j<20;j++){
for(int i=1;i+(1<<j)-1<=n;i++)st[j][i]=min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
int get(int l,int r){
int g=__lg(r-l+1);
return min(st[g][l],st[g][r-(1<<g)+1]);
}
int lcp(int x,int y){
x=rk[x],y=rk[y];
if(x==y)return n-x+1;
if(x>y)swap(x,y);
return get(x+1,y);
}
int query(int l1,int r1,int l2,int r2){
int len=lcp(l1,l2);
len=min({len,r1-l1+1,r2-l2+1});
if(len==min(r1-l1+1,r2-l2+1)){
if(r1-l1>r2-l2)return 1;
if(r1-l1<r2-l2)return -1;
if(r1-l1==r2-l2)return 0;
}
char p=s[l1+len],q=s[l2+len];
if(p>q)return 1;
if(p==q)return 0;
return -1;
}
void writ()
{
printf("%s\n",s+1);
for(int i=1;i<=n;i++)cout<<sa[i]<<" ";;cout<<"\n";
for(int i=1;i<=n;i++)cout<<ht[i]<<" ";;cout<<"\n";
for(int i=1;i<=n;i++)cout<<rk[i]<<" ";;cout<<"\n";
}
};
Suffix suf;
void solve()
{
int n,m,q;
cin>>n>>m>>q;
tri.init();
tri.tot=n;
for(int i=1;i<=n;i++){
int fa;char c;
cin>>fa>>c;
tri.trie[fa][c-'a']=i;
tri.f[0][i]=fa;
for(int j=1;j<=20;j++)tri.f[j][i]=tri.f[j-1][tri.f[j-1][i]];
}
string s;cin>>s;s="0"+s;
for(int i=1;i<=m;i++)suf.s[i]=s[i];
suf.s[m+1]=0;
suf.init();
vector<int> nxt(m+2,m),endp(m+2,-1);
set<int> se;
for(int i=1;i<=m;i++){
int rk=suf.rk[i];
int len=0,p=0;
auto it=se.lower_bound(rk);
if(it!=se.end()){
int id=suf.sa[*it];
int l=min(nxt[id]-id,suf.lcp(id,i));
if(l>len){
len=l;
p=tri.getkthfather(endp[id],nxt[id]-id-l);
}
}
if(it!=se.begin()){
it=prev(it);
int id=suf.sa[*it];
int l=min(nxt[id]-id,suf.lcp(id,i));
if(l>len){
len=l;
p=tri.getkthfather(endp[id],nxt[id]-id-l);
}
}
// cout<<"i="<<i<<" len="<<len<<endl;
for(int j=len;i+j<=m+1;j++){
if(i+j==m+1||tri.trie[p][s[i+j]-'a']==0){
nxt[i]=i+j;
endp[i]=p;
break;
}
else{
p=tri.trie[p][s[i+j]-'a'];
}
}
se.insert(rk);
}
// if(test){cout<<"nxt :";for(int i=1;i<=m;i++)cout<<nxt[i]<<" \n"[i==m];}
// if(test){cout<<"endp :";for(int i=1;i<=m;i++)cout<<endp[i]<<" \n"[i==m];}
vector<vector<int>> f(21,vector<int>(m+2,m+1));
for(int i=m;i>=1;i--){
f[0][i]=nxt[i];
for(int j=0;j<=19;j++)
f[j+1][i]=f[j][f[j][i]==m+1?m+1:f[j][i]+1];
}
while(q--){
int l,r;cin>>l>>r;
int ans1=0;
for(int i=20;i>=0;i--)if(f[i][l]<=r){
ans1+=(1<<i);
l=f[i][l]+1;
// cout<<"i="<<i<<" ans1="<<ans1<<" l="<<l<<endl;
}
int d=nxt[l]-1-r,p=endp[l];
for(int i=20;i>=0;i--)if(d>>i&1)p=tri.f[i][p];
cout<<ans1<<" "<<p<<"\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;cin>>T;while(T--)solve();
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20244kb
input:
1 5 10 5 0 a 0 b 1 a 1 c 2 a aaacbaacab 1 5 1 6 1 7 3 9 4 10
output:
2 2 2 5 3 0 2 1 4 0
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 554ms
memory: 22348kb
input:
1000 1000 1000 1000 0 a 1 a 2 b 3 a 4 b 5 a 6 b 7 b 8 a 9 b 10 b 11 b 12 b 13 b 14 b 15 a 16 a 17 b 18 a 19 a 20 b 21 a 22 b 23 a 24 b 25 a 26 b 27 b 28 b 29 b 30 a 31 b 32 a 33 a 34 a 35 b 36 b 37 b 38 a 39 a 40 b 41 a 42 b 43 a 44 b 45 b 46 a 47 a 48 a 49 a 50 b 51 b 52 a 53 b 54 b 55 a 56 a 57 b ...
output:
59 546 43 328 141 219 5 449 132 391 42 0 147 271 58 0 38 328 123 154 99 3 46 227 23 3 38 2 146 491 15 5 141 6 8 154 9 175 150 272 163 175 119 393 172 391 63 2 165 4 36 503 78 744 16 602 61 999 81 219 116 1 96 0 158 602 53 999 176 0 82 449 108 0 87 744 33 1 74 0 2 5 13 154 60 328 136 709 68 3 56 491 ...
result:
ok 1000000 lines
Test #3:
score: 0
Accepted
time: 1011ms
memory: 56388kb
input:
10 100000 100000 100000 0 a 1 b 2 b 3 b 4 b 5 b 6 b 7 b 8 b 9 a 10 b 11 a 12 a 13 a 14 b 15 b 16 b 17 b 18 a 19 a 20 b 21 a 22 b 23 a 24 b 25 a 26 b 27 a 28 b 29 a 30 a 31 b 32 b 33 a 34 a 35 a 36 a 37 a 38 b 39 b 40 b 41 b 42 b 43 b 44 a 45 a 46 a 47 a 48 a 49 b 50 b 51 b 52 a 53 a 54 b 55 a 56 b 5...
output:
307 77 696 39 292 33 1101 72778 797 7 923 59 530 22 175 7 751 2 641 23 999 54 771 60 1031 19 736 10 1828 25 1223 49 1119 8 1360 1 1010 25 186 10 634 11 1898 60 515 138 67 44 698 25 678 104 585 7 1179 34332 643 6 551 31 758 8 474 13 763 25 1042 85 1385 21 248 56 712 7 161 12 650 1 883 276 155 21 110 ...
result:
ok 1000000 lines
Test #4:
score: 0
Accepted
time: 1028ms
memory: 53916kb
input:
10 100000 100000 100000 0 a 1 b 2 a 3 b 4 b 5 a 6 b 7 a 8 b 9 b 10 a 11 b 12 a 13 b 14 b 15 a 16 b 17 a 18 b 19 b 20 a 21 b 22 a 23 b 24 b 25 a 26 b 27 a 28 b 29 b 30 a 31 b 32 a 33 b 34 b 35 a 36 b 37 a 38 b 39 b 40 a 41 b 42 a 43 b 44 b 45 a 46 b 47 a 48 b 49 b 50 a 51 b 52 a 53 b 54 b 55 a 56 b 5...
output:
1 5257 2 30770 1 27739 1 51805 2 6968 2 78118 1 7789 1 45259 2 35890 2 40254 2 908 1 10889 0 71302 2 10214 1 37077 2 18525 1 54529 1 25673 0 20925 1 35947 2 27976 2 28747 1 38487 1 15732 1 11502 2 33825 0 2153 2 64539 0 30535 1 70629 2 32885 0 58060 2 925 2 62115 1 10023 2 66651 1 21139 2 66936 0 28...
result:
ok 1000000 lines