QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620511#8044. Pattern Matching in A Minor "Low Space"rxzfn639ML 1ms3604kbC++23908b2024-10-07 18:42:102024-10-07 18:42:11

Judging History

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

  • [2024-10-07 18:42:11]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:3604kb
  • [2024-10-07 18:42:10]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int KMP(const string &s, const string &t) {
    int cnt = 0;
    int n = s.size(), m = t.size();
    vector<int> nxt(m + 1); // 前缀函数右移一位
    for (int i = 1, j = 0; i < m; i++) {
        while (j && t[i] != t[j]) j = nxt[j];
        if (t[i] == t[j]) j++;
        nxt[i + 1] = j;
    }
    for (int i = 0, j = 0; i < n; i++) {
        while (j && s[i] != t[j]) j = nxt[j];
        if (s[i] == t[j]) j++;
        if (j == m) {
            // return i - m + 1; // 查询出现位置
            cnt++;
            // j = 0; // 独立出现次数
            j = nxt[j]; // 不独立出现次数
        }
    }
    return cnt;
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int n;
    cin >> n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    cout << KMP(t, s) << '\n';
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3600kb

input:

3 7
aba
abababc

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3536kb

input:

1 11
a
abracadabra

output:

5

result:

ok 1 number(s): "5"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3544kb

input:

8 10
possible
impossible

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

10 21
pleasenote
theunusualmemorylimit

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: -100
Memory Limit Exceeded

input:

26 9999990
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzab...

output:

384615

result: