QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#286340#7618. Pattern SearchMisuki#TL 1ms3432kbC++203.5kb2023-12-17 17:21:012023-12-17 17:21:01

Judging History

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

  • [2023-12-17 17:21:01]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3432kb
  • [2023-12-17 17:21:01]
  • 提交

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);

    array<int, 26> cnt = {};
    for(int i = 0; i < 26; i++)
      cnt[i] = ft[i] / 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] % p; j++)
          r += (char)('a' + i);
        re[i] -= ft[i] % p;
      }
      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: 1ms
memory: 3432kb

input:

2
bajkaaall aal
abca cba

output:

2
1

result:

ok 2 number(s): "2 1"

Test #2:

score: -100
Time Limit Exceeded

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:


result: