QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#533488 | #7512. Almost Prefix Concatenation | shabi666 | WA | 21ms | 54944kb | C++20 | 3.7kb | 2024-08-26 01:20:13 | 2024-08-26 01:20:13 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define x first
#define y second
#define pb push_back
using namespace std;
typedef pair<int, int> PII;
const int maxn = 1e6 + 10, mod = 998244353;
string s, t;
int l, r, sz;
int ans[maxn], cnt[maxn], nxt[maxn];
int sans[maxn], scnt[maxn], snxt[maxn];
int ssans, sscnt, ssnxt;
typedef __int128 ull;
ull base = 131;
ull mod1 = 212370440130137957, mod2 = 1e9 + 7;
ull hs1[maxn], hs2[maxn], ht1[maxn], ht2[maxn];
ull pw1[maxn], pw2[maxn];
void init()
{
pw1[0] = pw2[0] = 1;
for (int i = 1; i < maxn; i++) pw1[i] = pw1[i - 1] * base % mod1;
for (int i = 1; i < maxn; i++) pw2[i] = pw2[i - 1] * base % mod2;
}
bool check(int ls, int rs, int lt, int rt) {
ull u1, u2, v1, v2;
if (ls == 0) {
u1 = hs1[rs], u2 = hs2[rs];
} else {
u1 = ( hs1[rs] + mod1 - hs1[ls - 1] * pw1[rs - ls + 1] % mod1 ) % mod1;
u2 = ( hs2[rs] + mod2 - hs2[ls - 1] * pw2[rs - ls + 1] % mod2 ) % mod2;
}
if (lt == 0) {
v1 = ht1[rt], v2 = ht2[rt];
} else {
v1 = ( ht1[rt] + mod1 - ht1[lt - 1] * pw1[rt - lt + 1] % mod1 ) % mod1;
v2 = ( ht2[rt] + mod2 - ht2[lt - 1] * pw2[rt - lt + 1] % mod2 ) % mod2;
}
return ( (u1 == v1) && (u2 == v2) );
}
int fd(int x) {
int l = 0, r = min( (int)s.size() - x, (int)t.size() ) + 1;
while (l + 1 < r) {
int mid = l + r >> 1;
if ( check(x, x + mid - 1, 0, mid - 1) ) l = mid;
else r = mid;
}
if ( l == min( (int)s.size() - x, (int)t.size() ) ) return l;
if ( l == min( (int)s.size() - x, (int)t.size() ) - 1 ) return l + 1;
int xx = x + l + 1;
int ll = 0, rr = min( (int)s.size() - xx, (int)t.size() - l - 1 ) + 1;
while (ll + 1 < rr) {
int mid = ll + rr >> 1;
if ( check(xx, xx + mid - 1, l + 1, l + mid) ) ll = mid;
else rr = mid;
}
return l + ll + 1;
}
void solve() {
cin >> s >> t;
hs1[0] = (ull)s[0] % mod1;
hs2[0] = (ull)s[0] % mod2;
for (int i = 1; i < (int)s.size(); i++) {
hs1[i] = (hs1[i - 1] * base + (ull)s[i]) % mod1;
hs2[i] = (hs2[i - 1] * base + (ull)s[i]) % mod2;
}
ht1[0] = (ull)t[0] % mod1;
ht2[0] = (ull)t[0] % mod2;
for (int i = 1; i < (int)t.size(); i++) {
ht1[i] = (ht1[i - 1] * base + (ull)t[i]) % mod1;
ht2[i] = (ht2[i - 1] * base + (ull)t[i]) % mod2;
}
l = r = (int)s.size() - 1, sz = 1;
ans[l] = 1, cnt[l] = 1, nxt[l] = 3;
ans[l + 1] = 0, cnt[l + 1] = 0, nxt[l + 1] = 0;
sans[l] = 1, scnt[l] = 1, snxt[l] = 3;
sans[l + 1] = 0, scnt[l + 1] = 0, snxt[l + 1] = 0;
ssans = 1, sscnt = 1, ssnxt = 3;
for (int i = (int)s.size() - 2; i >= 0; i--) {
l--, sz++;
int tmp1 = (ssans + ssnxt) % mod;
int tmp2 = sscnt;
int tmp3 = (ssnxt + 2 * sscnt) % mod;
ans[l] = tmp1;
ssans = (ssans + tmp1) % mod;
cnt[l] = tmp2;
sscnt = (sscnt + tmp2) % mod;
nxt[l] = tmp3;
ssnxt = (ssnxt + tmp3) % mod;
sans[l] = (sans[l + 1] + ans[l]) % mod;
scnt[l] = (scnt[l + 1] + cnt[l]) % mod;
snxt[l] = (snxt[l + 1] + nxt[l]) % mod;
int far = fd(i);
if ( sz != far ) {
sz = far;
r = l + sz - 1;
ssans = (sans[l] + mod - sans[r + 1]) % mod;
sscnt = (scnt[l] + mod - scnt[r + 1]) % mod;
ssnxt = (snxt[l] + mod - snxt[r + 1]) % mod;
r--, sz--;
}
}
cout << ssans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
init();
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 21ms
memory: 54944kb
input:
ababaab aba
output:
473
result:
ok 1 number(s): "473"
Test #2:
score: 0
Accepted
time: 20ms
memory: 54760kb
input:
ac ccpc
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: -100
Wrong Answer
time: 11ms
memory: 53592kb
input:
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...
output:
358271463
result:
wrong answer 1st numbers differ - expected: '75038697', found: '358271463'