QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#470010#7512. Almost Prefix Concatenation333zhanWA 1ms3840kbC++204.5kb2024-07-10 09:38:552024-07-10 09:38:56

Judging History

你现在查看的是最新测评结果

  • [2024-07-10 09:38:56]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3840kb
  • [2024-07-10 09:38:55]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

const int mod = 998244353;
const int mod2 = 1E9 + 9;
const int P = 1331;
const int P2 = 131;

inline int read () {
    int w = 1, s = 0; char ch = getchar ();
    for (; ! isdigit (ch); ch = getchar ()) if (ch == '-') w = -1;
    for (; isdigit (ch); ch = getchar ()) s = (s << 1) + (s << 3) + (ch ^ 48);
    return s * w;
}

void solve () {
    string s, t;
    cin >> s >> t;

    const int n = s.size ();
    const int m = t.size ();

    s = " " + s;
    t = " " + t;

    const int N = max (n, m);

    vector <int> p (N + 1);
    p[0] = 1;
    for (int i = 1; i <= N; i ++) {
        p[i] = p[i - 1] * P % mod;
    }

    vector <int> Hash1 (n + 1), Hash2 (m + 1);
    for (int i = 1; i <= n; i ++) {
        Hash1[i] = (Hash1[i - 1] * P + s[i]) % mod;
    }
    for (int i = 1; i <= m; i ++) {
        Hash2[i] = (Hash2[i - 1] * P + t[i]) % mod;
    }

    vector <int> p2 (N + 1);
    p2[0] = 1;
    for (int i = 1; i <= N; i ++) {
        p2[i] = p2[i - 1] * P2 % mod2;
    }

    vector <int> Hash21 (n + 1), Hash22 (m + 1);
    for (int i = 1; i <= n; i ++) {
        Hash21[i] = (Hash21[i - 1] * P2 + s[i]) % mod2;
    }
    for (int i = 1; i <= m; i ++) {
        Hash22[i] = (Hash22[i - 1] * P2 + t[i]) % mod2;
    }


    vector <int> lcp (n + 1);
    for (int i = 1; i <= n; i ++) {
        auto check = [&] (int x) {
            int l1 = i, r1 = x;
            int l2 = 1, r2 = x - i + 1;
            // if (i == 3) {
            //     cerr << l1 << " " << r1 << " " << l2 << " " << r2 << '\n';
            // }
            int len = r1 - l1 + 1;
            int a = (Hash1[r1] - Hash1[l1 - 1] * p[len] % mod + mod) % mod;
            int b = (Hash2[r2] - Hash2[l2 - 1] * p[len] % mod + mod) % mod;
            int a2 = (Hash21[r1] - Hash21[l1 - 1] * p2[len] % mod2 + mod2) % mod2;
            int b2 = (Hash22[r2] - Hash22[l2 - 1] * p2[len] % mod2 + mod2) % mod2;
            return a == b && a2 == b2;
        };
        auto check2 = [&] (int l1, int r1, int l2, int r2) {
            int len = r1 - l1 + 1;
            int a = (Hash1[r1] - Hash1[l1 - 1] * p[len] % mod + mod) % mod;
            int b = (Hash2[r2] - Hash2[l2 - 1] * p[len] % mod + mod) % mod;
            int a2 = (Hash21[r1] - Hash21[l1 - 1] * p2[len] % mod2 + mod2) % mod2;
            int b2 = (Hash22[r2] - Hash22[l2 - 1] * p2[len] % mod2 + mod2) % mod2;
            return a == b && a2 == b2;
        };

        int l = i, r = min (n, i + m - 1);
        // if (i == 3) {
        //     cerr << l << " " << r << '\n';
        // }
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (check (mid)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        // if (i == 3) {
        //     cerr << l << '\n';
        // }
        if (l + 1 >= n) {
            lcp[i] = min (n, i + m - 1);
            continue;
        }
        if (l + 1 >= i + m - 1) {
            lcp[i] = i + m - 1;
            continue;
        }
        if (! check (l)) {
            l --;
        }
        
        // cerr << i << " " << l << '\n';
        int l1 = l + 2, l2 = l - i + 1;
        l = l + 2, r = min (n, i + m - 1);
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (check2 (l1, mid, l2, l2 + mid - l1)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        if (! check2 (l1, l1, l2, l2)) {
            l --;
        }
        lcp[i] = l;
    }

    // for (int i = 1; i <= n; i ++) {
    //     cerr << lcp[i] << " ";
    // }

    vector <int> suf1 (n + 3), suf2 (n + 3), suf3 (n + 3);
    suf1[n + 1] = 1;

    for (int i = n; i >= 1; i --) {
        int l = i + 1, r = lcp[i] + 1;
        suf1[i] = (suf1[l] - suf1[r + 1] + mod) % mod;
        suf2[i] = ((suf2[l] - suf2[r + 1] + mod) % mod + suf1[i]) % mod;
        suf3[i] = ((suf3[l] - suf3[r + 1] + mod) % mod + 2 * (suf2[l] - suf2[r + 1] + mod) % mod + suf1[i]) % mod;
        suf1[i] = (suf1[i] + suf1[i + 1]) % mod;
        suf2[i] = (suf2[i] + suf2[i + 1]) % mod;
        suf3[i] = (suf3[i] + suf3[i + 1]) % mod;
    }

    cout << (suf3[1] - suf3[2] + mod) % mod;
} 

signed main () {
	ios::sync_with_stdio (false);
    cin.tie (nullptr);
    
	int T = 1; 
	// cin >> T;
	// T = read ();

	while (T --) {
		solve ();
	}
	
	return 0;
}
/*
3 2 5 4 6 7 7
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3824kb

input:

ababaab
aba

output:

473

result:

ok 1 number(s): "473"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3840kb

input:

ac
ccpc

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3660kb

input:

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

75038697

result:

ok 1 number(s): "75038697"

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3608kb

input:

lvvvllvllvllvllllllllvvvllvlllvvlvlvllvlvvlvvvvlvvllllllvvlvlvvlllvvlvlvllllllvlvvvvvvlllvvvllvlvvvlvvlllvvvvvvlvlllvvvvlvvvvvlvvlvvlllvvllvvllvlvlvlvlvllllvvllvvllvlllvvvllllvvlvvllvvvvlvlvvlvvlllvvvvvvvvlvvlvlllvllvvvvllvvvlvvvvvvlvlllvllllvllllllllvvllllllvlvvlvvvlvllllvllvlvvllllllvlvvvlvlvlvvvl...

output:

718752703

result:

wrong answer 1st numbers differ - expected: '538419149', found: '718752703'