結果

問題 No.437 cwwゲーム
ユーザー te-sh
提出日時 2017-10-30 12:03:32
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,178 bytes
コンパイル時間 610 ms
コンパイル使用メモリ 97,996 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 22:14:22
合計ジャッジ時間 1,861 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto s = readln.chomp.map!(c => cast(int)(c - '0')).array, n = s.length;

  auto dp = new int[](1<<n);
  foreach (d; 0..1<<n) {
    if (d.popcnt < 3 || d.popcnt % 3 != n % 3) continue;
    foreach (i; 0..n) {
      if (!d.bitTest(i) || s[i] == 0) continue;
      foreach (j; i+1..n) {
        if (!d.bitTest(j) || s[i] == s[j]) continue;
        foreach (k; j+1..n) {
          if (!d.bitTest(k) || s[j] != s[k]) continue;
          auto r = s[i] * 100 + s[j] * 10 + s[k];
          dp[d] = max(dp[d], r + dp[d.bitComp(i).bitComp(j).bitComp(k)]);
        }
      }
    }
  }

  writeln(dp.maxElement);
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
0