結果
| 問題 |
No.1859 ><<<
|
| ユーザー |
tnakao0123
|
| 提出日時 | 2022-02-28 16:27:29 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 47 ms / 2,000 ms |
| コード長 | 2,404 bytes |
| コンパイル時間 | 361 ms |
| コンパイル使用メモリ | 45,312 KB |
| 実行使用メモリ | 15,744 KB |
| 最終ジャッジ日時 | 2024-07-06 18:54:22 |
| 合計ジャッジ時間 | 5,063 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
/* -*- 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;
}
tnakao0123