結果

問題 No.238 Mr. K's Another Gift
ユーザー rsk0315rsk0315
提出日時 2019-02-03 02:00:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 862 ms / 2,000 ms
コード長 1,833 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 70,680 KB
実行使用メモリ 4,608 KB
最終ジャッジ日時 2023-08-20 07:53:55
合計ジャッジ時間 17,771 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 30 ms
4,380 KB
testcase_05 AC 658 ms
4,384 KB
testcase_06 AC 431 ms
4,540 KB
testcase_07 AC 732 ms
4,476 KB
testcase_08 AC 730 ms
4,560 KB
testcase_09 AC 509 ms
4,496 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 14 ms
4,380 KB
testcase_14 AC 500 ms
4,380 KB
testcase_15 AC 862 ms
4,500 KB
testcase_16 AC 862 ms
4,544 KB
testcase_17 AC 858 ms
4,540 KB
testcase_18 AC 860 ms
4,488 KB
testcase_19 AC 859 ms
4,472 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 22 ms
4,380 KB
testcase_25 AC 31 ms
4,376 KB
testcase_26 AC 434 ms
4,608 KB
testcase_27 AC 582 ms
4,596 KB
testcase_28 AC 5 ms
4,540 KB
testcase_29 AC 558 ms
4,492 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 5 ms
4,380 KB
testcase_33 AC 62 ms
4,376 KB
testcase_34 AC 763 ms
4,376 KB
testcase_35 AC 859 ms
4,492 KB
testcase_36 AC 860 ms
4,536 KB
testcase_37 AC 857 ms
4,556 KB
testcase_38 AC 857 ms
4,540 KB
testcase_39 AC 857 ms
4,564 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>

template <intmax_t X=943681729, intmax_t MOD=1000000007>
class rolling_hash {
  std::vector<intmax_t> hash;
  intmax_t offset(size_t len) const {
    intmax_t res = 1;
    for (intmax_t dbl = X%MOD; len; len >>= 1) {
      if (len & 1)
        res = res*dbl % MOD;

      dbl = dbl*dbl % MOD;
    }

    return res;
  }

public:
  rolling_hash(const std::string &s): hash(s.length()+1) {
    for (size_t i = 0; i < s.length(); ++i)
      hash[i+1] = (hash[i]*X+s[i]) % MOD;
  }

  intmax_t get_base() const { return X; }
  intmax_t get_mod() const { return MOD; }

  intmax_t prefix(size_t i) const {
    // hash of prefix s[0..i-1] (inclusive)
    assert(0 < i);
    return hash[i];
  }

  intmax_t substr(size_t i, size_t j) const {
    // hash of substring s[i..j-1] (inclusive)
    assert(i < j);
    intmax_t res=hash[j];
    res = (res - offset(j-i)*hash[i]) % MOD;
    if (res < 0) res += MOD;
    return res;
  }

  intmax_t inserted(size_t i, int ch) const {
    intmax_t res = 0;
    if (i > 0) res += prefix(i);
    res = (res*X + ch) % MOD;
    size_t n = hash.size()-1;
    if (i < n) res = (res*offset(n-i) + substr(i, n)) % MOD;
    return res;
  }

  void debug() const {
    for (size_t i=0; i<hash.size(); ++i)
      fprintf(stderr, "%jd%c", hash[i], i+1<hash.size()? ' ':'\n');
  }
};

int main() {
  char buf[131072];
  scanf("%s", buf);
  std::string s = buf;
  std::string t = s;
  std::reverse(t.begin(), t.end());
  size_t n = s.length();

  rolling_hash<> r1(s), r2(t);
  for (size_t i = 0; i <= n; ++i)
    for (int c = 'a'; c <= 'z'; ++c)
      if (r1.inserted(i, c) == r2.inserted(n-i, c))
        return !printf("%s%c%s\n", s.substr(0, i).c_str(), c, s.substr(i).c_str());

  puts("NA");
}
0