QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#455787 | #7618. Pattern Search | ucup-team3924# | WA | 0ms | 3560kb | C++14 | 1.7kb | 2024-06-26 19:52:23 | 2024-06-26 19:52:23 |
Judging History
answer
#include<bits/stdc++.h>
std::vector<int> kmp(std::string& str) {
int n = str.size();
std::vector<int> v(1 + n);
v[0] = -1;
v[1] = 0;
for (int i = 1; i <= n; i++) {
v[i] = v[i - 1] + 1;
while (v[i] > 0 && str[i - 1] != str[v[i] - 1])
v[i] = v[v[i] - 1] + 1;
}
return v;
}
void solve_test() {
std::string a, b;
std::cin >> a >> b;
std::vector<int> fr_a(26), fr_b(26);
for (int i = 0; i < a.size(); i++)
fr_a[a[i] - 'a']++;
for (int i = 0; i < b.size(); i++)
fr_b[b[i] - 'a']++;
b.clear();
std::string patt;
for (int i = 0; i < 26; i++) {
int half = fr_b[i] / 2;
for (int j = 0; j < half; j++)
b.push_back('a' + i);
}
patt = b;
for (int i = 0; i < 26; i++) {
if (fr_b[i] % 2 == 1)
b.push_back(i + 'a');
}
b = b + patt;
auto kmp_b = kmp(b);
int longest = 0, res = 0;
for (int i = 0; i < a.size(); i++) {
int best = -1;
int chosen = -1;
for (int j = 0; j < 26; j++) {
if (fr_a[j] > 0) {
int len = longest + 1;
if (len > b.size()) {
len = kmp_b[b.size()] + 1;
}
while (len > 0 && j + 'a' != b[len - 1])
len = kmp_b[len - 1] + 1;
if (len > best) {
best = len;
chosen = j;
}
}
}
if (best == b.size())
res++;
longest = best;
fr_a[chosen]--;
}
std::cout << res << "\n";
}
int main(){
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
int t;
std::cin >> t;
while (t--) solve_test();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3560kb
input:
2 bajkaaall aal abca cba
output:
2 1
result:
ok 2 number(s): "2 1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3556kb
input:
16 a a a b b a aa a ab aa ab b ab c aaz az abcde edcba aaaaaaaaaaaabbb aaaaaaaaabb aaaaaazz az aaaaaaaaaz zzzzz gggggggggggggggggggge ggggeeee hyphyphyphyphyphyphyphyphyphyphyphyp eeeeeeeeee hyphyphyphyphyphyphyphyphyphyphyphype eeteeteeteet aaaabbbbbbcccccccc aaabbbbbcccccc
output:
1 0 0 2 0 1 0 1 1 1 2 0 0 0 0 1
result:
wrong answer 10th numbers differ - expected: '2', found: '1'