提出 #7564275


ソースコード 拡げる

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = (a); i < (b); i++)
typedef uint64_t ull;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;

using namespace std;

class RollingHash {
 public:
  explicit RollingHash(const std::string &s) {
    mod = std::vector<ll>{999999937LL, 1000000007LL};
    base = 9973;
    n = s.size();
    for (int i = 0; i < MS; i++) {
      hs[i].assign(n+1, 0);
      pw[i].assign(n+1, 0);
      hs[i][0] = 0;
      pw[i][0] = 1;
      for (int j = 0; j < n; j++) {
        pw[i][j+1] = pw[i][j]*base%mod[i];
        hs[i][j+1] = (hs[i][j]*base+s[j])%mod[i];
      }
    }
  }
  bool match(int l1, int r1, int l2, int r2) {
    bool ret = 1;
    for (int i = 0; i < MS; i++)
      ret &= hash(l1, r1, i) == hash(l2, r2, i);
    return ret;
  }

  std::vector<ll> get_hash(int l, int r) {
    std::vector <ll> res(MS);
    for (int i = 0; i < MS; i++)
      res[i] = hash(l, r, i);
    return res;
  }

 private:
  static const int MAX = 500000, MS = 2;
  std::vector<ll> mod;
  ll base;
  int n;
  std::vector<ll> hs[MS], pw[MS];
  ll hash(int l, int r, int i) {
    return ((hs[i][r]-hs[i][l]*pw[i][r-l])%mod[i]+mod[i])%mod[i];
  }

  bool match(int l, int r, ll h[]) {
    bool ret = 1;
    for (int i = 0; i < MS; i++)
      ret &= hash(l, r, i) == h[i];
    return ret;
  }
};

signed main() {
  ll N;
  string s;
  cin >> N >> s;
  RollingHash rh(s);
  ll ng = 2501, ok = 0;
  while (ok + 1 < ng) {
    ll m = (ok+ng)/2;
    // cout << m << endl;
    bool t = false;

    map<vector<ll>, ll> mp; // mp[hash value] = index
    rep(i,N) {
      if (i+m > N)
        continue;

      auto h = rh.get_hash(i, i+m);
      auto it = mp.find(h);
      if (it == mp.end()) {
        mp[h] = i;
      } else {
        // hash find
        if (it->second + m <= i) {
          t = true;
          break;
        }
      }
    }

    if (t) {
      ok = m;
    } else {
      ng = m;
    }
  }

  cout << ok << endl;
  return 0;
}

提出情報

提出日時
問題 E - Who Says a Pun?
ユーザ bobuhiro11
言語 C++14 (GCC 5.4.1)
得点 500
コード長 2111 Byte
結果 AC
実行時間 24 ms
メモリ 896 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 3
AC × 70
セット名 テストケース
Sample 00-sample-00, 00-sample-01, 00-sample-02
All 00-sample-00, 00-sample-01, 00-sample-02, 01-handmade-03, 01-handmade-04, 01-handmade-05, 01-handmade-06, 01-handmade-07, 01-handmade-08, 01-handmade-09, 01-handmade-10, 01-handmade-11, 01-handmade-12, 02-binary-13, 02-binary-14, 02-binary-15, 02-binary-16, 02-binary-17, 02-binary-18, 02-binary-19, 02-binary-20, 02-binary-21, 02-binary-22, 02-binary-23, 03-BigRandom-24, 03-BigRandom-25, 03-BigRandom-26, 03-BigRandom-27, 03-BigRandom-28, 03-BigRandom-29, 03-BigRandom-30, 03-BigRandom-31, 03-BigRandom-32, 03-BigRandom-33, 03-BigRandom-34, 03-BigRandom-35, 03-BigRandom-36, 03-BigRandom-37, 03-BigRandom-38, 03-BigRandom-39, 03-BigRandom-40, 03-BigRandom-41, 03-BigRandom-42, 03-BigRandom-43, 03-BigRandom-44, 03-BigRandom-45, 03-BigRandom-46, 03-BigRandom-47, 03-BigRandom-48, 03-BigRandom-49, 03-BigRandom-50, 03-BigRandom-51, 03-BigRandom-52, 03-BigRandom-53, 03-BigRandom-54, 04-zero-55, 04-zero-56, 05-AllRandom-57, 05-AllRandom-58, 05-AllRandom-59, 05-AllRandom-60, 05-AllRandom-61, 05-AllRandom-62, 05-AllRandom-63, 05-AllRandom-64, 05-AllRandom-65, 05-AllRandom-66, 05-AllRandom-67, 05-AllRandom-68, 05-AllRandom-69
ケース名 結果 実行時間 メモリ
00-sample-00 AC 1 ms 256 KiB
00-sample-01 AC 1 ms 256 KiB
00-sample-02 AC 1 ms 256 KiB
01-handmade-03 AC 4 ms 384 KiB
01-handmade-04 AC 17 ms 768 KiB
01-handmade-05 AC 14 ms 640 KiB
01-handmade-06 AC 14 ms 640 KiB
01-handmade-07 AC 14 ms 640 KiB
01-handmade-08 AC 4 ms 384 KiB
01-handmade-09 AC 19 ms 896 KiB
01-handmade-10 AC 19 ms 896 KiB
01-handmade-11 AC 7 ms 640 KiB
01-handmade-12 AC 10 ms 640 KiB
02-binary-13 AC 20 ms 768 KiB
02-binary-14 AC 21 ms 896 KiB
02-binary-15 AC 17 ms 768 KiB
02-binary-16 AC 21 ms 896 KiB
02-binary-17 AC 22 ms 896 KiB
02-binary-18 AC 18 ms 896 KiB
02-binary-19 AC 15 ms 768 KiB
02-binary-20 AC 16 ms 768 KiB
02-binary-21 AC 16 ms 768 KiB
02-binary-22 AC 14 ms 768 KiB
02-binary-23 AC 14 ms 768 KiB
03-BigRandom-24 AC 12 ms 640 KiB
03-BigRandom-25 AC 12 ms 640 KiB
03-BigRandom-26 AC 14 ms 640 KiB
03-BigRandom-27 AC 16 ms 768 KiB
03-BigRandom-28 AC 17 ms 768 KiB
03-BigRandom-29 AC 15 ms 768 KiB
03-BigRandom-30 AC 18 ms 768 KiB
03-BigRandom-31 AC 14 ms 640 KiB
03-BigRandom-32 AC 18 ms 768 KiB
03-BigRandom-33 AC 17 ms 768 KiB
03-BigRandom-34 AC 14 ms 640 KiB
03-BigRandom-35 AC 15 ms 768 KiB
03-BigRandom-36 AC 16 ms 768 KiB
03-BigRandom-37 AC 12 ms 640 KiB
03-BigRandom-38 AC 13 ms 640 KiB
03-BigRandom-39 AC 13 ms 640 KiB
03-BigRandom-40 AC 14 ms 768 KiB
03-BigRandom-41 AC 14 ms 640 KiB
03-BigRandom-42 AC 14 ms 640 KiB
03-BigRandom-43 AC 12 ms 640 KiB
03-BigRandom-44 AC 14 ms 640 KiB
03-BigRandom-45 AC 15 ms 768 KiB
03-BigRandom-46 AC 14 ms 640 KiB
03-BigRandom-47 AC 17 ms 768 KiB
03-BigRandom-48 AC 13 ms 640 KiB
03-BigRandom-49 AC 14 ms 768 KiB
03-BigRandom-50 AC 15 ms 768 KiB
03-BigRandom-51 AC 13 ms 640 KiB
03-BigRandom-52 AC 14 ms 768 KiB
03-BigRandom-53 AC 14 ms 768 KiB
03-BigRandom-54 AC 14 ms 640 KiB
04-zero-55 AC 1 ms 256 KiB
04-zero-56 AC 1 ms 256 KiB
05-AllRandom-57 AC 24 ms 896 KiB
05-AllRandom-58 AC 23 ms 896 KiB
05-AllRandom-59 AC 22 ms 896 KiB
05-AllRandom-60 AC 23 ms 896 KiB
05-AllRandom-61 AC 21 ms 896 KiB
05-AllRandom-62 AC 23 ms 896 KiB
05-AllRandom-63 AC 23 ms 896 KiB
05-AllRandom-64 AC 21 ms 896 KiB
05-AllRandom-65 AC 23 ms 896 KiB
05-AllRandom-66 AC 21 ms 896 KiB
05-AllRandom-67 AC 22 ms 896 KiB
05-AllRandom-68 AC 24 ms 896 KiB
05-AllRandom-69 AC 24 ms 896 KiB