結果

問題 No.1859 ><<<
ユーザー CleyLCleyL
提出日時 2023-07-06 12:23:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 2,315 bytes
コンパイル時間 1,541 ms
コンパイル使用メモリ 93,036 KB
実行使用メモリ 57,400 KB
最終ジャッジ日時 2023-09-27 13:34:28
合計ジャッジ時間 20,653 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
26,756 KB
testcase_01 AC 8 ms
26,808 KB
testcase_02 AC 7 ms
26,752 KB
testcase_03 AC 8 ms
26,756 KB
testcase_04 AC 384 ms
57,296 KB
testcase_05 AC 380 ms
57,384 KB
testcase_06 AC 386 ms
57,312 KB
testcase_07 AC 375 ms
57,304 KB
testcase_08 AC 390 ms
57,348 KB
testcase_09 AC 373 ms
57,400 KB
testcase_10 AC 59 ms
31,580 KB
testcase_11 AC 213 ms
44,232 KB
testcase_12 AC 182 ms
42,044 KB
testcase_13 AC 105 ms
36,024 KB
testcase_14 AC 224 ms
45,716 KB
testcase_15 AC 320 ms
52,588 KB
testcase_16 AC 361 ms
55,000 KB
testcase_17 AC 263 ms
48,048 KB
testcase_18 AC 348 ms
54,552 KB
testcase_19 AC 253 ms
47,164 KB
testcase_20 AC 319 ms
52,908 KB
testcase_21 AC 369 ms
56,588 KB
testcase_22 AC 363 ms
55,320 KB
testcase_23 AC 315 ms
52,196 KB
testcase_24 AC 371 ms
56,048 KB
testcase_25 AC 368 ms
54,996 KB
testcase_26 AC 335 ms
53,228 KB
testcase_27 AC 362 ms
54,792 KB
testcase_28 AC 355 ms
53,992 KB
testcase_29 AC 335 ms
53,584 KB
testcase_30 AC 329 ms
52,368 KB
testcase_31 AC 326 ms
52,348 KB
testcase_32 AC 377 ms
56,380 KB
testcase_33 AC 319 ms
52,360 KB
testcase_34 AC 342 ms
54,220 KB
testcase_35 AC 380 ms
56,904 KB
testcase_36 AC 379 ms
56,548 KB
testcase_37 AC 327 ms
52,836 KB
testcase_38 AC 396 ms
57,140 KB
testcase_39 AC 394 ms
56,648 KB
testcase_40 AC 361 ms
55,800 KB
testcase_41 AC 378 ms
56,592 KB
testcase_42 AC 315 ms
52,128 KB
testcase_43 AC 351 ms
55,100 KB
testcase_44 AC 368 ms
55,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
using namespace std;

struct charSet{
  int base = 2;
  int encode(char c){
    return (c == '<' ? 0 : 1);
  }
};

struct RollingHash{
  charSet C;
  string t;
  const long long MOD1 = 1000000007;
  const long long MOD2 = 998244353;
  vector<int> stringIdx[1000001];
  map<pair<long long, long long>, int> hashIdx;
  int idxSize;

  RollingHash(string s, int n){
    idxSize = 0;
    long long val1 = 0;
    long long val2 = 0;
    long long baseMOD1 = 1;
    long long baseMOD2 = 1;
    for(int i = 0; n > i; i++){
      baseMOD1 = (baseMOD1*C.base)%MOD1;
      baseMOD2 = (baseMOD2*C.base)%MOD2;
      val1 = (val1*C.base + C.encode(s[i]))%MOD1;
      val2 = (val2*C.base + C.encode(s[i]))%MOD2;
    }
    for(int i = n; s.size() >= i; i++){
      if(!hashIdx[make_pair(val1, val2)]){
        hashIdx[make_pair(val1, val2)] = hashIdx.size();
      }
      stringIdx[hashIdx[make_pair(val1, val2)]].push_back(i-n);
      if(i!=s.size()){
        val1 = ((val1*C.base + C.encode(s[i]))%MOD1 - (baseMOD1 * C.encode(s[i-n]))%MOD1 + MOD1)%MOD1;
        val2 = ((val2*C.base + C.encode(s[i]))%MOD2 - (baseMOD2 * C.encode(s[i-n]))%MOD2 + MOD2)%MOD2;
      }
    }
    idxSize = hashIdx.size();
  }

  RollingHash(string s, string x): RollingHash(s, (int)x.size()){
    t = x;
  }

  pair<long long, long long> hash(string x){
    long long val1 = 0;
    long long val2 = 0;
    for(int i = 0; x.size() > i; i++){
      val1 = (val1*C.base + C.encode(x[i]))%MOD1;
      val2 = (val2*C.base + C.encode(x[i]))%MOD2;
    }
    return make_pair(val1, val2);
  }

  vector<int> search(){
    return search(t);
  }

  vector<int> search(string x){
    auto hashedString = hash(x);
    return stringIdx[hashIdx[hashedString]];
  }

  int distMax(){
    int distMx = 0;
    for(int i = 1; idxSize >= i; i++){
      distMx = max(distMx, stringIdx[i][stringIdx[i].size()-1]-stringIdx[i][0]);
    }
    return distMx;
  }
};


int main(){
  int n;cin>>n;
  vector<int> A(n);
  for(int i = 0; n > i; i++){
    cin>>A[i];
  }
  string c = "";
  for(int i = 0; 2*n > i;i++){
    c.push_back((A[(i)%n] < A[(i+1)%n] ? '<' : '>'));
  }
  string s;cin>>s;
  RollingHash B(c, n-1);
  auto z = B.search(s);
  if(z.size()){
    cout << z[0] << endl;
  }else{
    cout << -1 << endl;
  }
}
0