QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#199983 | #7345. Circular Shift | nameless_story# | WA | 2ms | 10036kb | C++20 | 1.5kb | 2023-10-04 14:54:28 | 2023-10-04 14:54:28 |
Judging History
answer
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const ll N=620000;
ll p,np,cnt;
ll fa[N],c[N][30],mx[N],rt[N],R[N],pre[30][N],bb[N],b[N];
void extend(ll w,ll k,ll w2)
{
p=np; mx[np=++cnt]=mx[p]+1;
rt[np]=k;
R[np]=1<<w2;
for (; p&&!c[p][w]; p=fa[p]) c[p][w]=np;
if (!p) fa[np]=1;
else
{
ll q=c[p][w];
if (mx[q]==mx[p]+1) fa[np]=q;
else
{
ll nq=++cnt; mx[nq]=mx[p]+1;
memcpy(c[nq],c[q],sizeof c[nq]);
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
for (; p&&c[p][w]==q; p=fa[p]) c[p][w]=nq;
}
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
string st; cin>>st;
ll n=st.size();
np=cnt=1;
for (ll i=0; i<n; ++i)
{
extend(st[i]-'a',i,(i==n-1?30:st[i+1]-'a'));
}
for (ll i=1; i<=cnt; ++i) ++bb[mx[i]];
for (ll i=1; i<=n; ++i) bb[i]+=bb[i-1];
for (ll i=1; i<=cnt; ++i) b[bb[mx[i]]--]=i;
for (ll k=0; k<26; ++k)
{
for (ll i=0; i<n; ++i)
{
pre[k][i+1]=pre[k][i]+(st[i]-'a'==k);
}
}
ll ans=0;
for (ll i=cnt; i>1; --i)
{
ll x=b[i];
ll l=rt[x]-mx[x]+1,r=rt[x]-mx[fa[x]];
// cerr<<rt[x]<<' '<<l<<' '<<r<<endl;
for (ll j=0; j<26; ++j)
{
if (R[x]>>j&1)
{
ll num=pre[j][r]-pre[j][l];
ans+=num;
}
}
R[fa[x]]|=R[x];
rt[fa[x]]=rt[x];
}
for (ll i=cnt; i>1; --i)
{
ll x=b[i];
ll l=rt[x]-mx[x]+1,r=rt[x]-mx[fa[x]];
ll w=st[r]-'a';
if (R[fa[x]]>>w&1) ++ans;
}
// for (ll k=0; k<30; ++k) if (pre[k][n]) ++ans;
cout<<ans<<'\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 10036kb
input:
abaac
output:
7
result:
ok 1 number(s): "7"
Test #2:
score: 0
Accepted
time: 1ms
memory: 9880kb
input:
aaa
output:
3
result:
ok 1 number(s): "3"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 9884kb
input:
a
output:
0
result:
wrong answer 1st numbers differ - expected: '1', found: '0'