QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#200644 | #7345. Circular Shift | Delay_for_five_minutes | WA | 0ms | 16364kb | C++14 | 3.2kb | 2023-10-04 18:09:57 | 2023-10-04 18:09:57 |
Judging History
answer
#include<bits/stdc++.h>
#define maxn 600004
int mp[26][26];
std::string str;
struct SAM {
int cnt,c[maxn][27], fa[maxn], len[maxn],last;
int sz[maxn],fir[maxn],to[maxn],nxt[maxn],e;
int tag[maxn];
int end[maxn];
SAM(){cnt = last = 1, e = 0;}
void ext(int ch,int L,int tg) {
int p = last, np = last = ++cnt, q, nq;
end[np] = L - 1;
len[np] = L, sz[np] = 1;
tag[np] = tg;
for(;!c[p][ch];p = fa[p]) c[p][ch] = np;
if (!p) {fa[np] = 1;return ;}
q = c[p][ch];
if (len[p] + 1 == len[q]) {
fa[np] = q;return ;
}
nq = ++cnt;
len[nq] = len[p] + 1;
for(int i = 0; i < 26;i++) {
c[nq][i] = c[q][i];
}
tag[nq] = tag[q];
end[nq] = end[q];
fa[nq] =fa[q], fa[q] = fa[np] = nq;
for(;c[p][ch] == q;p = fa[p]) {
c[p][ch] = nq;
}
}
bool cmp(int A,int B) {
return len[A] < len[B] || (len[A] == len[B] && A < B);
}
void pre() {
int id[maxn];
for(int i=1;i<=cnt;i++) {
id[i] = i;
}
std::sort(id + 1,id + 1 + cnt,[&](int A,int B) {
return len[A] < len[B] || (len[A] == len[B] && A < B);
});
for(int i = cnt; i>=1;i--) {
tag[fa[id[i]]] |= tag[id[i]];
}
}
long long f[maxn][27];
long long tp() {
int d[maxn];
memset(d,0,sizeof d);
long long ans = 0;
/*
for(int i=1;i<=cnt;i++) {
for(int j = 0; j < 26;j++) if (c[i][j]) {
printf("%d->%d(%c)\n",i,c[i][j],'a'+j);
// d[c[i][j]]++;
}
}
for(int i=1;i<=cnt;i++) {
printf("%d",i);
for(int j = 0; j < 26;j++) if (tag[i] & (1<<j)) {
putchar('a' + j);
}
putchar('\n');
}
*/
for(int i=1;i<=cnt;i++) {
for(int j = 0; j < 26;j++) if (c[i][j]) {
// printf("%d->%d(%c)\n",i,c[i][j],'a'+j);
d[c[i][j]]++;
}
}
std::queue<int> que;
for(int i = 0 ; i < 26; i++) if (c[1][i]){
f[c[1][i]][i] = 1;
d[c[1][i]]--;
if (d[c[1][i]] == 0) {
que.push(c[1][i]);
}
}
while(!que.empty()) {
int now = que.front();
que.pop();
for(int i = 0; i < 26; i++) {
if (tag[now] & (1 << i)) {
ans = (ans + f[now][i]);
// printf("%d %c%d\n",now,'a'+i,f[now][i]);
}
else {
if (i + 'a' == str[end[now] - len[fa[now]]]) {
if (tag[fa[now]] & (1 << i)) {
ans ++;
// printf("%d %c%d\n",now,'a'+i,1);
}
}
}
if (i + 'a' == str[end[now] - len[fa[now]]] && len[fa[now]] == 0 && (tag[fa[now]] & (1 << i))) {
ans --;
}
}
for(int i = 0; i < 26; i++) if (c[now][i]) {
d[c[now][i]]--;
for(int j = 0; j < 26; j++) {
f[c[now][i]][j] = (f[c[now][i]][j] + f[now][j]);
}
if (d[c[now][i]] == 0) {
que.push(c[now][i]);
}
}
}
return ans;
}
}s;
int main() {
// freopen("in.txt","r",stdin);
std::cin >> str;
str.push_back('a' + 26);
for(int i = 0; i < str.length() - 1; i++) {
// printf("%d\n",i);
s.ext(str[i] - 'a', i + 1, 1 << (str[i + 1] - 'a'));
}
for(int i = 1; i < str.length(); i++) {
mp[str[i - 1] - 'a'][str[i] - 'a'] ++;
}
str.pop_back();
bool t[27];
long long Ans = 0;
for(auto i : str) {
if (!t[i - 'a']) {
Ans ++;
t[i - 'a'] = 1;
}
}
s.pre();
Ans = (Ans + s.tp());
std::cout << Ans << std::endl;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 16364kb
input:
abaac
output:
4
result:
wrong answer 1st numbers differ - expected: '7', found: '4'