結果

問題 No.1859 ><<<
ユーザー emthrm
提出日時 2022-02-26 22:56:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 2,877 bytes
コンパイル時間 1,976 ms
コンパイル使用メモリ 200,228 KB
最終ジャッジ日時 2025-01-28 03:33:21
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename T = std::string>
struct RollingHash {
  T s;

  explicit RollingHash(const T& s, const int base = 10007,
                       const int mod = 1000000007)
      : base(base), mod(mod), hash({0}), power({1}) {
    const int n = s.size();
    this->s.reserve(n);
    hash.reserve(n + 1);
    power.reserve(n + 1);
    add(s);
  }

  long long get(const int left, const int right) const {
    const long long res =
        hash[right] - hash[left] * power[right - left] % mod;
    return res < 0 ? res + mod : res;
  }

  void add(const T& t) {
    for (const auto c : t) {
      s.push_back(c);
      const int hash_nxt = (hash.back() * base % mod + c) % mod;
      hash.emplace_back(hash_nxt);
      const int power_nxt = power.back() * base % mod;
      power.emplace_back(power_nxt);
    }
  }

  int longest_common_prefix(const int i, const int j) const {
    const int n = s.size();
    int lb = 0, ub = n + 1 - std::max(i, j);
    while (ub - lb > 1) {
      const int mid = (lb + ub) >> 1;
      (get(i, i + mid) == get(j, j + mid) ? lb : ub) = mid;
    }
    return lb;
  }

  template <typename U>
  int longest_common_prefix(const RollingHash<U>& t,
                            const int i, const int j) const {
    int lb = 0;
    int ub = std::min(static_cast<int>(s.size()) - i,
                      static_cast<int>(t.str.size()) - j)
             + 1;
    while (ub - lb > 1) {
      const int mid = (lb + ub) >> 1;
      (get(i, i + mid) == t.get(j, j + mid) ? lb : ub) = mid;
    }
    return lb;
  }

 private:
  const int base, mod;
  std::vector<long long> hash, power;
};

int main() {
  int n; cin >> n;
  vector<int> a(n); REP(i, n) cin >> a[i];
  string t = "";
  FOR(i, 1, n * 2) t += (a[(i - 1) % n] < a[i % n] ? '<' : '>');
  string s; cin >> s;
  RollingHash rh_t(t), rh_s(s);
  REP(i, n) {
    if (rh_t.get(i, i + n - 1) == rh_s.get(0, n - 1)) {
      cout << i << '\n';
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
0