結果

問題 No.1859 ><<<
ユーザー tnakao0123tnakao0123
提出日時 2022-02-28 16:27:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,404 bytes
コンパイル時間 383 ms
コンパイル使用メモリ 44,912 KB
実行使用メモリ 15,616 KB
最終ジャッジ日時 2023-09-21 00:08:33
合計ジャッジ時間 7,132 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
13,596 KB
testcase_01 AC 4 ms
13,524 KB
testcase_02 AC 4 ms
13,496 KB
testcase_03 AC 4 ms
13,520 KB
testcase_04 AC 47 ms
15,604 KB
testcase_05 AC 48 ms
15,588 KB
testcase_06 AC 48 ms
15,596 KB
testcase_07 AC 48 ms
15,556 KB
testcase_08 AC 47 ms
15,612 KB
testcase_09 AC 47 ms
15,604 KB
testcase_10 AC 11 ms
14,096 KB
testcase_11 AC 30 ms
15,252 KB
testcase_12 AC 26 ms
15,144 KB
testcase_13 AC 17 ms
14,588 KB
testcase_14 AC 32 ms
15,296 KB
testcase_15 AC 42 ms
15,432 KB
testcase_16 AC 45 ms
15,548 KB
testcase_17 AC 35 ms
15,316 KB
testcase_18 AC 44 ms
15,584 KB
testcase_19 AC 34 ms
15,292 KB
testcase_20 AC 41 ms
15,480 KB
testcase_21 AC 45 ms
15,536 KB
testcase_22 AC 43 ms
15,508 KB
testcase_23 AC 40 ms
15,480 KB
testcase_24 AC 45 ms
15,532 KB
testcase_25 AC 44 ms
15,596 KB
testcase_26 AC 42 ms
15,464 KB
testcase_27 AC 45 ms
15,532 KB
testcase_28 AC 43 ms
15,576 KB
testcase_29 AC 43 ms
15,512 KB
testcase_30 AC 41 ms
15,420 KB
testcase_31 AC 41 ms
15,472 KB
testcase_32 AC 46 ms
15,536 KB
testcase_33 AC 41 ms
15,460 KB
testcase_34 AC 43 ms
15,480 KB
testcase_35 AC 47 ms
15,548 KB
testcase_36 AC 46 ms
15,548 KB
testcase_37 AC 42 ms
15,436 KB
testcase_38 AC 48 ms
15,616 KB
testcase_39 AC 47 ms
15,560 KB
testcase_40 AC 44 ms
15,584 KB
testcase_41 AC 45 ms
15,560 KB
testcase_42 AC 40 ms
15,480 KB
testcase_43 AC 44 ms
15,552 KB
testcase_44 AC 45 ms
15,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1859.cc:  No.1859 ><<< - yukicoder
 */

#include<cstdio>
#include<cstring>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 300000;
const int MAX_N2 = MAX_N * 2;
const int P = 4073;
const int MOD = 1000000009;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }

  bool operator==(const MI m) { return v == m.v; }
};

typedef MI<MOD> mi;

/* global variables */

mi pes[MAX_N2 + 1], invpes[MAX_N2 + 1];
mi srh[MAX_N2 + 1], trh[MAX_N2 + 1];
int as[MAX_N2];
char s[MAX_N + 4], t[MAX_N2 + 4];

/* subroutines */

inline void prep_rhash(int n) {
  pes[0] = invpes[0] = 1;
  pes[1] = P;
  invpes[1] = pes[1].inv();
  for (int i = 2; i <= n; i++) {
    pes[i] = pes[i - 1] * P;
    invpes[i] = invpes[i - 1] * invpes[1];
  }
}

inline int s2rh(mi rh[], const char s[]) {
  int k = 0;
  for (; s[k]; k++) rh[k + 1] = rh[k] + mi(s[k]) * pes[k];
  return k;
}

inline mi s2hash(const char s[]) {
  mi h = 0;
  for (int k = 0; s[k]; k++) h += mi(s[k]) * pes[k];
  return h;
}

inline mi rhash(mi rhs[], int i, int j) {
  return (rhs[j] - rhs[i]) * invpes[i];
}

/* main */

int main() {
  int n;
  scanf("%d", &n);
  int n2 = n * 2;
  for (int i = 0; i < n; i++) scanf("%d", as + i), as[n + i] = as[i];
  scanf("%s", s);

  for (int i = 0; i + 1 < n2; i++)
    t[i] = (as[i] < as[i + 1]) ? '<' : '>';
  t[n2 - 1] = '\0';

  prep_rhash(n2);
  s2rh(trh, t);
  mi sh = s2hash(s);

  int k = -1;
  for (int i = 0; i < n; i++)
    if (rhash(trh, i, i + n - 1) == sh &&
	! strncmp(s, t + i, n - 1)) {
      k = i;
      break;
    }

  printf("%d\n", k);
  return 0;
}
0