結果

問題 No.1646 Avoid Palindrome
ユーザー 👑 obakyanobakyan
提出日時 2021-08-13 22:16:37
言語 Lua
(LuaJit 2.1.1696795921)
結果
TLE  
実行時間 -
コード長 2,114 bytes
コンパイル時間 428 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-14 18:29:29
合計ジャッジ時間 46,509 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,756 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 2,983 ms
6,940 KB
testcase_05 AC 2,912 ms
6,944 KB
testcase_06 AC 2,834 ms
6,940 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 AC 2,815 ms
6,940 KB
testcase_10 TLE -
testcase_11 AC 2,996 ms
6,940 KB
testcase_12 AC 2,913 ms
6,944 KB
testcase_13 AC 2,912 ms
6,940 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

local mfl, mce = math.floor, math.ceil
local mod = 998244353

local n = io.read("*n", "*l")
local s = io.read()
local t = {}
for i = 1, n do
  t[i] = s:sub(i, i)
end
for i = 1, n - 1 do
  if t[i] == t[i + 1] and t[i] ~= "?" then
    print(0) os.exit()
  end
end
for i = 1, n - 2 do
  if t[i] == t[i + 2] and t[i] ~= "?" then
    print(0) os.exit()
  end
end
if n == 1 then
  print(t[1] == "?" and 26 or 1) os.exit()
end
local dp1, dp2 = {}, {}
if t[1] == "?" and t[2] == "?" then
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    dp1[v] = i == j and 0 or 1
  end end
elseif t[1] == "?" then
  local i2 = t[2]:byte() - 96
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    if i == i2 or j ~= i2 then
      dp1[v] = 0
    else
      dp1[v] = 1
    end
  end end
elseif t[2] == "?" then
  local i1 = t[1]:byte() - 96
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    if i ~= i1 or j == i1 then
      dp1[v] = 0
    else
      dp1[v] = 1
    end
  end end
else
  local i1 = t[1]:byte() - 96
  local i2 = t[2]:byte() - 96
  for i = 1, 26 do for j = 1, 26 do
    local v = (i - 1) * 26 + j
    if i ~= i1 or j ~= i2 then
      dp1[v] = 0
    else
      dp1[v] = 1
    end
  end end
end
for i = 3, n do
  local src = i % 2 == 1 and dp1 or dp2
  local dst = i % 2 == 1 and dp2 or dp1
  for j = 1, 676 do dst[j] = 0 end
  for prv_state = 1, 676 do
    local srcval = src[prv_state]
    local prvprv = mce(prv_state / 26)
    local prv = prv_state - (prvprv - 1) * 26
    if t[i] == "?" then
      for j = 1, 26 do
        if j ~= prvprv and j ~= prv then
          local d = (prv - 1) * 26 + j
          dst[d] = dst[d] + srcval
        end
      end
    else
      local z = t[i]:byte() - 96
      for j = 1, 26 do
        if j == z and j ~= prvprv and j ~= prv then
          local d = (prv - 1) * 26 + j
          dst[d] = dst[d] + srcval
        end
      end
    end
  end
  for j = 1, 676 do dst[j] = dst[j] % mod end
end
local tbl = n % 2 == 1 and dp2 or dp1
local ret = 0
for i = 1, 676 do
  ret = ret + tbl[i]
end
print(ret % mod)
-- print(os.clock())
0