QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#286344#7618. Pattern SearchMisuki#WA 68ms3520kbC++203.5kb2023-12-17 17:34:102023-12-17 17:34:11

Judging History

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

  • [2023-12-17 17:34:11]
  • 评测
  • 测评结果:WA
  • 用时:68ms
  • 内存:3520kb
  • [2023-12-17 17:34:10]
  • 提交

answer

#pragma GCC optimize("O2")
#include <algorithm>
#include <array>
#include <bit>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <compare>
#include <complex>
#include <concepts>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numbers>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <ranges>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tuple>
#include <type_traits>
#include <variant>

//#define int ll
#define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1)
#define INT128_MIN (-INT128_MAX - 1)

namespace R = std::ranges;
namespace V = std::views;

using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = tuple<int, int, int>;
using ldb = long double;
//#define double ldb

template<class T>
ostream& operator<<(ostream& os, const pair<T, T> pr) {
  return os << pr.first << ' ' << pr.second;
}
template<class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N> &arr) {
  for(const T &X : arr)
    os << X << ' ';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &vec) {
  for(const T &X : vec)
    os << X << ' ';
  return os;
}

/**
 * template name: zAlgo
 * author: Misuki
 * last update: 2022/02/04
 */

vector<int> build(string s) {
  vector<int> z(ssize(s));
  z[0] = s.size();
  for(int i = 1, l = 0, r = -1; i < s.size(); i++) {
    if (i <= r)
      z[i] = min(r - i + 1, z[i - l]);
    while(i + z[i] < s.size() and s[i + z[i]] == s[z[i]])
      l = i, r = i + z[i], z[i] += 1;
  }
  return z;
}

array<int, 26> toFreq(string s) {
  array<int, 26> res = {};
  for(char c : s)
    res[c - 'a']++;
  return res;
}

string calc(array<int, 26> f, string p) {
  string res;
  for(int i = 0; f[p[i % ssize(p)] - 'a']; i++)
    res += p[i % ssize(p)], f[p[i % ssize(p)] - 'a']--;
  return res;
}


signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);

  int z; cin >> z;
  while(z--) {
    string s, t; cin >> s >> t;

    array<int, 26> fs = toFreq(s), ft = toFreq(t);

    int p = INT_MAX;
    for(int x : ft)
      if (x > 0)
        p = min(p, x + 1);

    array<int, 26> cnt = {};
    for(int i = 0; i < 26; i++)
      cnt[i] = (ft[i] + p - 1) / p;

    string r;
    {
      array<int, 26> re = cnt;
      for(int i = 0; i < 26; i++) {
        if (cnt[i] == 0) continue;
        for(int j = 0; j < ft[i] % cnt[i]; j++)
          r += (char)('a' + i);
        re[i] -= ft[i] % cnt[i];
      }
      for(int i = 0; i < 26; i++)
        while(re[i]--)
          r += (char)('a' + i);
    }

    string ss = calc(fs, r);
    string tt = calc(ft, r);

    auto z = build(tt + '#' + ss);
    int ans = 0;
    for(int i = ssize(tt) + 1; i < ssize(z); i++)
      ans += z[i] == ssize(tt);

    cout << ans << '\n';
  }

  return 0;
}

详细

Test #1:

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

input:

2
bajkaaall aal
abca cba

output:

2
1

result:

ok 2 number(s): "2 1"

Test #2:

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

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
2
2
0
0
0
0
1

result:

ok 16 numbers

Test #3:

score: -100
Wrong Answer
time: 68ms
memory: 3520kb

input:

90522
cyykzyylklyll ylcyllklzk
ttusuuudtdtqus uuddu
uefyqfkiblyfkyd ffyyqde
qfxqecljeqeedea jqdxf
prrbfxdxffpbpp ffppd
ynjgygygjnjnjg jgynjggn
maenpaksmxyya saxkep
nrdnbnjipnjowjz djbwojzrpni
oputuoufoojupu uoouopo
mphmhphpkpkpmhp phmhpppp
zwznzpzqyjczzy wczjnpzqy
pfxfxxkfffpfx fxffkffxpx
hzdhzhhh h...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
4
1
1
1
2
1
1
4
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
2
1
1
1
1
1
2
4
1
1
1
1
1
1
1
1
1
2
2
2
2
1
1
1
3
1
1
1
1
4
1
2
1
1
1
1
1
3
2
1
3
1
1
1
1
1
1
1
1
1
1
1
3
1
1
4
1
1
1
1
1
1
1
1
1
1
5
1
7
1
1
1
1
1
3
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
2
1
1
1
1
1
1
1
2
1
1
1
1
2
...

result:

wrong answer 43rd numbers differ - expected: '1', found: '2'