結果

問題 No.238 Mr. K's Another Gift
ユーザー rsk0315rsk0315
提出日時 2019-02-03 02:00:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 606 ms / 2,000 ms
コード長 1,833 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 68,804 KB
最終ジャッジ日時 2025-01-06 20:45:33
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:64:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |   scanf("%s", buf);
      |   ~~~~~^~~~~~~~~~~

ソースコード

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