結果

問題 No.197 手品
ユーザー te-sh
提出日時 2017-05-10 17:35:50
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 942 bytes
コンパイル時間 577 ms
コンパイル使用メモリ 98,320 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-12 19:06:47
合計ジャッジ時間 1,534 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto s1 = toInt(readln.chomp);
  auto n = readln.chomp.to!size_t;
  auto s2 = toInt(readln.chomp);

  writeln(calc(s1, s2, n) ? "SUCCESS" : "FAILURE");
}

auto toInt(string s)
{
  auto r = 0;
  foreach (c; s) {
    r <<= 1;
    r |= c == 'o' ? 1 : 0;
  }
  return r;
}

auto calc(int s1, int s2, size_t n)
{
  auto t1 = s1.popcnt, t2 = s2.popcnt;
  if (t1 != t2) return true;
  if (t1 == 0 || t1 == 3) return false;
  if (t1 == 2) { s1 = ~s1 & 7; s2 = ~s2 & 7; }

  auto i1 = s1.bsf, i2 = s2.bsf;
  if (n == 0)
    return i1 != i2;
  else if (n == 1)
    return i1 == 1 ? i2 == 1 : (i1 != i2 && i2 != 1);
  else
    return false;
}

pragma(inline) {
  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