QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455212 | #7008. Rikka with Nice Counting Striking Back | wdnmdwrnmmp | AC ✓ | 3273ms | 212244kb | C++14 | 5.4kb | 2024-06-25 21:07:34 | 2024-06-25 21:07:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef __int128 ll;
const int base=36634229, P=1e18L+3;
namespace runs{ // code from: https://loj.ac/s/2043370
const int N=2e5+5,B=131,mod=998244353;
int n,tot,cnt,a[N],pw[N],hs[N],ly[2][N];
char s[N];
struct ss{int l,r,p;}xl[2*N],xl1[2*N];
bool cmp(ss x,ss y){return (x.l==y.l?x.r<y.r:x.l<y.l);}
int js(int x,int y,int z){return (hs[x+z-1]-hs[y+z-1]+(hs[y-1]-hs[x-1])*pw[z])%mod;}
int lcp(int l,int r){
int le=0,ri=min(n-l+1,n-r+1);
while(le<ri){
int mid=(le+ri+1)/2;
if(js(l,r,mid)==0) le=mid; else ri=mid-1;
}return le;
}
int lcs(int l,int r){
int le=0,ri=min(l,r);
while(le<ri){
int mid=(le+ri+1)/2;
if(js(l-mid+1,r-mid+1,mid)==0) le=mid; else ri=mid-1;
}return le;
}
void sol(int opt){
stack<int> st;
per(i,n,1){
while(st.size()){
int dq=lcp(st.top(),i);
if(a[st.top()+dq]>a[i+dq]) st.pop(); else break;
}ly[opt][i]=(st.size()?st.top()-1:n),st.push(i);
int dl=i,dr=ly[opt][i],d2=dr+lcp(dl,dr+1),d1=dl-lcs(dl-1,dr);
if(d2-d1+1>=2*(dr-dl+1)) xl[++tot]={d1,d2,dr-dl+1};
}
}
vector< array<int,3> > work(const string &_s){
fill(a, a+n+5, 0);
fill(pw, pw+n+5, 0);
fill(hs, hs+n+5, 0);
fill(ly[0], ly[0]+n+5, 0);
fill(ly[1], ly[1]+n+5, 0);
fill(s, s+n+5, 0);
fill(xl, xl+n*2+10, (ss){0,0,0});
fill(xl1, xl1+n*2+10, (ss){0,0,0});
// memset(a, 0, sizeof(a));
// memset(pw, 0, sizeof(pw));
// memset(hs, 0 ,sizeof(hs));
// memset(ly, 0, sizeof(ly));
// memset(s, 0, sizeof(s));
// memset(xl, 0, sizeof(xl));
// memset(xl1, 0, sizeof(xl1));
n=0, tot=0, cnt=0;
rep(i,0,(int)_s.size()-1){
s[i+1]=_s[i];
}
n=strlen(s+1),pw[0]=1;
rep(i,1,n) a[i]=s[i]-'a'+1,pw[i]=pw[i-1]*B%mod,hs[i]=(hs[i-1]*B+a[i])%mod;
sol(0); rep(i,1,n) a[i]=27-a[i]; sol(1);
sort(xl+1,xl+tot+1,cmp);
rep(i,1,tot) if(i==1||xl[i].l!=xl[i-1].l||xl[i].r!=xl[i-1].r) xl1[++cnt]=xl[i];
vector< array<int,3> > res(cnt);
rep(i,0,cnt-1){
res[i]={xl1[i+1].l, xl1[i+1].r, xl1[i+1].p};
}
return res;
}
}
namespace hashtable{
const int N=1e7, P=1e7+19;
struct Set{
int key[N], bg[P], nxt[N], tot;
void clear(){
rep(i,1,tot){
bg[key[i]%P]=0;
}
tot=0;
}
bool count(int x){
for(int id=bg[x%P]; id; id=nxt[id]){
if(key[id]==x){
return 1;
}
}
return 0;
}
void ins(int x){
if(!count(x)){
tot++;
key[tot]=x, nxt[tot]=bg[x%P];
bg[x%P]=tot;
}
}
};
}
using hashtable::Set;
Set S;
namespace sam{
const int N=2e5+5, M=N*2;
array<int,26> s[M];
int fa[M], len[M], tot;
void init(){
rep(i,0,tot){
s[i].fill(0);
fa[i]=0;
len[i]=0;
}
tot=1;
}
int ins(int p,int c){
int u=++tot;
len[u]=len[p]+1;
for(; p && !s[p][c]; p=fa[p]){
s[p][c]=u;
}
if(p==0){
fa[u]=1;
}
else{
int v=s[p][c];
if(len[v]==len[p]+1){
fa[u]=v;
}
else{
int t=++tot;
s[t]=s[v], fa[t]=fa[v], len[t]=len[p]+1;
fa[u]=fa[v]=t;
for(; p && s[p][c]==v; p=fa[p]){
s[p][c]=t;
}
}
}
return u;
}
int countall(const string &s){
init();
int lst=1;
for(char c:s){
lst=ins(lst, c-'a');
}
int res=0;
rep(i,2,tot){
res+= len[i]-len[fa[i]];
}
return res;
}
}
void solve(){
string s; cin>>s;
int n=s.size();
vi hsh(n), pw(n);
hsh[0]=s[0], pw[0]=1;
rep(i,1,n-1){
hsh[i]=((ll)hsh[i-1]*base+s[i])%P;
pw[i]=(ll)pw[i-1]*base%P;
}
auto val=[&](int l,int r){
if(l==0){
return hsh[r];
}
int res=(hsh[r]-(ll)hsh[l-1]*pw[r-l+1])%P;
if(res<0){
res+=P;
}
return (int)res;
};
int ans=sam::countall(s);
vector< array<int,3> > run=runs::work(s);
S.clear();
for(auto x:run){
rep(y, x[0], x[0]+x[2]-1){
for(int z=y+x[2]*2-1; z<=x[1]; z+=x[2]){
S.ins(val(y-1,z-1));
}
}
}
ans-= S.tot;
cout<< ans <<'\n';
}
signed main(){
#ifndef ONLINE_JUDGE
assert(freopen(".in","r",stdin));
#endif
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t; cin>>t;
while(t--){
solve();
}
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 24048kb
input:
6 rikkasuggeststoallthecontestants thisisaproblemdesignedforgrandmasters ifyoudidnotachievethat youdbetterskiptheproblem wishyouahighrank enjoytheexperience
output:
500 679 244 290 132 163
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 2653ms
memory: 212244kb
input:
1000 mxsslrrxtwwnwlsrxxxbsmtxxwwntsbrwlxnbxunutnlmrrlubturuwmbnobrolwunwurtbnmlumxmwtwrsulruxslsnltsstmmxbsmuonroubrnnmu sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss glggglgg yykkyyyykykykyyyykyykykkkyyyykkyykkkyyykykyykyyykkykky...
output:
6522 1 20 9443 11294 1 8619 26 7898 260905 9048 6 22114 52 20 45 7 39 10 1 28 26 10 47 32 12977 30 13 7473 12 8402 1 8083 16 1 10462 16 9278 1 1 8968 7858 11148 8130 6 8516 12223 9266 8374 26 45 14 10150 9 10175 298758 203677 11522 12 8985 10687 12 1 6613277686 10 10 1 5794 28 1 280529 7874 13 10564...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 3253ms
memory: 194696kb
input:
26 hihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihhihhihihhihihhihhihihhihihhihhihihh...
output:
9523687993 9529757593 9537405289 9539347561 9533035177 9528058105 564250 9522959641 9542382361 9518953705 9519439273 9534977449 9519803449 9535705801 9519560665 9534249097 9527572537 9523687993 9539468953 9532064041 9525873049 9532185433 9541168441 9524901913 9531092905 9518832313
result:
ok 26 lines
Test #4:
score: 0
Accepted
time: 3273ms
memory: 193476kb
input:
26 oohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohoohooohoohoohooohoohooohoohoohooohoohooohooh...
output:
9625902365 9628810517 9622671085 9626467839 9628891299 9626306275 9630668503 9620409189 9618228075 9622428739 9628406607 9625336891 9630426157 9626871749 9620086061 9626144711 9616935563 9617177909 9626790967 9627194877 9626467839 354971 9616370089 9618308857 9617824165 9616773999
result:
ok 26 lines
Test #5:
score: 0
Accepted
time: 2808ms
memory: 192304kb
input:
26 vggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvgvvggvgvggvgvvgvggvgvggvgvvgvggvgvvggvgvggvgvvgvggvgvggvg...
output:
13085098340 13073668733 13071665606 13067070197 13077910649 13074964874 13078735466 13070840789 13072726085 13067895014 669720 13065891887 13065302732 13076496677 13068484169 13064242253 13065420563 13063181774 13080502931 13067070197 13072490423 13070015972 13083802199 13064831408 13075671860 13085...
result:
ok 26 lines