結果

問題 No.238 Mr. K's Another Gift
ユーザー rsk0315rsk0315
提出日時 2019-04-10 20:42:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,749 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 70,004 KB
実行使用メモリ 6,204 KB
最終ジャッジ日時 2023-09-22 08:27:07
合計ジャッジ時間 3,805 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 30 ms
5,612 KB
testcase_06 AC 22 ms
6,136 KB
testcase_07 AC 34 ms
6,076 KB
testcase_08 AC 33 ms
6,048 KB
testcase_09 AC 26 ms
6,124 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 24 ms
4,840 KB
testcase_15 AC 38 ms
6,108 KB
testcase_16 AC 38 ms
6,072 KB
testcase_17 AC 39 ms
6,120 KB
testcase_18 AC 39 ms
6,116 KB
testcase_19 AC 38 ms
6,116 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 4 ms
4,376 KB
testcase_26 AC 22 ms
6,140 KB
testcase_27 AC 28 ms
6,108 KB
testcase_28 AC 7 ms
6,160 KB
testcase_29 AC 27 ms
6,144 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 5 ms
4,376 KB
testcase_34 AC 34 ms
5,808 KB
testcase_35 AC 38 ms
6,204 KB
testcase_36 AC 38 ms
6,140 KB
testcase_37 AC 38 ms
6,136 KB
testcase_38 AC 38 ms
6,048 KB
testcase_39 AC 38 ms
6,100 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 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, offset;

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

    offset[0] = 1;
    for (size_t i = 1; i <= s.length(); ++i)
      offset[i] = offset[i-1] * X % 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.rbegin(), s.rend());
  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