QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#526283 | #7876. Cyclic Substrings | AmiyaCast | WA | 2ms | 11952kb | C++14 | 2.1kb | 2024-08-21 12:56:21 | 2024-08-21 12:56:22 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define pii make_pair
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=b;i>=a;--i)
const ll inf = 1145141919810;
using namespace std;
inline ll read(){
ll x=0,f=1;
char c=getchar();
while (c<'0' || c>'9'){
if (c=='-') f=-1;
c=getchar();
}
while (c>='0' && c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
inline void print(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) print(x / 10);
putchar(x % 10 + '0');
return ;
}
inline void pprint(ll x){print(x); puts("");}
const int N = 6e6 + 7;
struct PAM {
//功能多但是开多了就炸
//时间复杂度O(n)
//注意extent传入整数
int t[N];//cnt表示出现次数
int tot, lst = 0;
int fa[N], len[N], tr[N][15], cnt[N], pos[N];
#define sfor(x, i) for(int i = 0; i <= 14; ++i) if(tr[x][i])
PAM() {
len[1] = -1, len[0] = 0;
fa[0] = 1;
fa[1] = 1;
tot = 1, lst = 1;
}
int getfail(int x, int i) {
while(t[i - len[x] - 1] != t[i]) {
x = fa[x];
}
return x;
}
void extend(int x, int i) { //插入节点
t[i] = x;
int f = getfail(lst, i);//找要接在哪个节点下面
int j = tr[f][x];
if(!j) {
j = ++tot;
tr[f][x] = j;
len[j] = len[f]+2;
int tmp = getfail(fa[f], i);
if(f != 1) fa[j] = tr[tmp][x];
}
lst = j;//更新lst
// cout << lst << endl;
if(!pos[lst]) pos[lst] = i;
cnt[lst]++;
}
void sum() {
for(int i = tot; i >= 0; --i) {
cnt[fa[i]] += cnt[i];
}
}
}pam;
const ll mod = 998244353;
char s[N];
int main() {
int n = read();
scanf("%s", s + 1);
for(int i = n + 1; i <= 2 * n; ++i) s[i] = s[i - n];
for(int i = 1; i <= n * 2; ++i){
pam.extend(s[i] - '0' + 1, i);
}
ll ans = 0;
pam.sum();
rep(i, 2, pam.tot){
if(pam.pos[i] <= n) pam.cnt[i] = (pam.cnt[i] + 1) / 2;
if(pam.len[i] > n) continue;
// cout << pam.pos[i] - pam.len[i] + 1 << " " << pam.pos[i] << " " <<endl;
ans = 1ll * (ans + 1ll * pam.len[i] * pam.cnt[i] % mod * pam.cnt[i] % mod) % mod;
}
// puts("");
pprint(ans);
}
/*
5
01010
*/
/*
8
00110011
*/
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 11884kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 1ms
memory: 11804kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 2ms
memory: 11876kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 11936kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 11952kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 1ms
memory: 11920kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: -100
Wrong Answer
time: 0ms
memory: 11868kb
input:
3 666
output:
39
result:
wrong answer 1st numbers differ - expected: '54', found: '39'