QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#455774#7618. Pattern Searchucup-team3924#WA 0ms3740kbC++141.7kb2024-06-26 19:34:422024-06-26 19:34:46

Judging History

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

  • [2024-06-26 19:34:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3740kb
  • [2024-06-26 19:34:42]
  • 提交

answer

#include<bits/stdc++.h>

std::vector<int> kmp(std::string& str) {
    int n = str.size();
    std::vector<int> v(n);

    v[0] = 0;

    for (int i = 1; i < n; i++) {
        v[i] = v[i - 1] + 1;
        while (v[i] > 0 && str[i] != str[v[i] - 1])
            v[i] = v[v[i] - 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] + 1;
                while (len > 0 && j + 'a' != b[len - 1])
                    len = kmp_b[len - 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;
}

详细

Test #1:

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

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: 3404kb

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'