QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#548337 | #7512. Almost Prefix Concatenation | retired_midlights# | WA | 0ms | 18248kb | C++14 | 2.8kb | 2024-09-05 17:18:05 | 2024-09-05 17:18:05 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)a; i <= (int)b; i ++)
#define per(i, a, b) for(int i = (int)a; i >= (int)b; i --)
#define ll long long
#define ld long double
using namespace std;
const int maxn = 1000010, base1 = 127, base2 = 131, mod1 = 1e9 + 7, mod2 = 1e9 + 9, mod = 998244353;
char s[maxn], t[maxn];
int n, m, power1[maxn], power2[maxn], hsh1_s[maxn], hsh1_t[maxn], hsh2_s[maxn], hsh2_t[maxn];
int f[maxn], g[maxn], h[maxn], suf_f[maxn], suf_g[maxn], suf_h[maxn];
int gethsh1_s(int l, int r) {
return (hsh1_s[r] - 1ll * hsh1_s[l - 1] * power1[r - l + 1] % mod1 + mod1) % mod1;
}
int gethsh1_t(int l, int r) {
return (hsh1_t[r] - 1ll * hsh1_t[l - 1] * power1[r - l + 1] % mod1 + mod1) % mod1;
}
int gethsh2_s(int l, int r) {
return (hsh2_s[r] - 1ll * hsh2_s[l - 1] * power2[r - l + 1] % mod2 + mod2) % mod2;
}
int gethsh2_t(int l, int r) {
return (hsh2_t[r] - 1ll * hsh2_t[l - 1] * power2[r - l + 1] % mod2 + mod2) % mod2;
}
bool check(int l1, int r1, int l2, int r2) {
if(r1 > n || r2 > m) return 0;
return gethsh1_s(l1, r1) == gethsh1_t(l2, r2) && gethsh2_s(l1, r1) == gethsh2_t(l2, r2);
}
int main() {
scanf("%s", s + 1);
scanf("%s", t + 1);
n = strlen(s + 1), m = strlen(t + 1);
power1[0] = power2[0] = 1;
rep(i, 1, max(n, m)) {
power1[i] = 1ll * power1[i - 1] * base1 % mod1;
power2[i] = 1ll * power2[i - 1] * base2 % mod2;
}
rep(i, 1, n) {
hsh1_s[i] = (1ll * hsh1_s[i - 1] * base1 + s[i] - 'a') % mod1;
hsh2_s[i] = (1ll * hsh2_s[i - 1] * base2 + s[i] - 'a') % mod2;
}
rep(i, 1, n) {
hsh1_t[i] = (1ll * hsh1_t[i - 1] * base1 + t[i] - 'a') % mod1;
hsh2_t[i] = (1ll * hsh2_t[i - 1] * base2 + t[i] - 'a') % mod2;
}
suf_f[n + 1] = 1;
per(i, n, 1) {
int l = i, r = min(n, i + m - 1), pos = min(n + 1, i + m);
while(l <= r) {
int mid = (l + r) >> 1;
if(check(i, mid, 1, mid - i + 1)) l = mid + 1;
else r = mid - 1, pos = mid;
}
l = pos + 1, r = min(n + 1, i + m - 1);
while(l <= r) {
int mid = (l + r) >> 1;
if(check(i, mid, 1, mid - i + 1)) l = mid + 1;
else r = mid - 1, pos = mid;
}
// pos --;
// printf("i : %d pos : %d\n", i, pos);
pos ++;
f[i] = (suf_f[i + 1] - suf_f[pos] + mod) % mod;
g[i] = ((suf_g[i + 1] - suf_g[pos] + mod) % mod + f[i]) % mod;
h[i] = ((suf_h[i + 1] - suf_h[pos] + mod) % mod + 2 * g[i] % mod - f[i]) % mod;
suf_f[i] = (suf_f[i + 1] + f[i]) % mod;
suf_g[i] = (suf_g[i + 1] + g[i]) % mod;
suf_h[i] = (suf_h[i + 1] + h[i]) % mod;
// printf("i : %d f : %d\n", i, f[i]);
}
printf("%d\n", h[1]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18248kb
input:
ababaab aba
output:
473
result:
ok 1 number(s): "473"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 18152kb
input:
ac ccpc
output:
4
result:
wrong answer 1st numbers differ - expected: '5', found: '4'