QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#620511 | #8044. Pattern Matching in A Minor "Low Space" | rxzfn639 | ML | 1ms | 3604kb | C++23 | 908b | 2024-10-07 18:42:10 | 2024-10-07 18:42:11 |
Judging History
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