/* -*- coding: utf-8 -*- * * 1859.cc: No.1859 ><<< - yukicoder */ #include #include #include 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 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 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; }