結果

問題 No.1646 Avoid Palindrome
ユーザー 👑 obakyanobakyan
提出日時 2021-08-13 22:48:33
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 474 ms / 3,000 ms
コード長 2,374 bytes
コンパイル時間 399 ms
コンパイル使用メモリ 7,076 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 03:21:56
合計ジャッジ時間 14,230 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 3 ms
6,944 KB
testcase_04 AC 343 ms
6,940 KB
testcase_05 AC 337 ms
6,940 KB
testcase_06 AC 338 ms
6,940 KB
testcase_07 AC 371 ms
6,940 KB
testcase_08 AC 341 ms
6,940 KB
testcase_09 AC 321 ms
6,944 KB
testcase_10 AC 332 ms
6,944 KB
testcase_11 AC 327 ms
6,940 KB
testcase_12 AC 341 ms
6,944 KB
testcase_13 AC 344 ms
6,948 KB
testcase_14 AC 422 ms
6,944 KB
testcase_15 AC 433 ms
6,944 KB
testcase_16 AC 422 ms
6,940 KB
testcase_17 AC 443 ms
6,944 KB
testcase_18 AC 429 ms
6,940 KB
testcase_19 AC 427 ms
6,944 KB
testcase_20 AC 430 ms
6,940 KB
testcase_21 AC 440 ms
6,944 KB
testcase_22 AC 428 ms
6,940 KB
testcase_23 AC 449 ms
6,940 KB
testcase_24 AC 460 ms
6,940 KB
testcase_25 AC 458 ms
6,944 KB
testcase_26 AC 459 ms
6,940 KB
testcase_27 AC 457 ms
6,944 KB
testcase_28 AC 457 ms
6,944 KB
testcase_29 AC 300 ms
6,940 KB
testcase_30 AC 301 ms
6,944 KB
testcase_31 AC 474 ms
6,944 KB
testcase_32 AC 335 ms
6,944 KB
testcase_33 AC 302 ms
6,940 KB
testcase_34 AC 472 ms
6,940 KB
testcase_35 AC 1 ms
6,944 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 1 ms
6,944 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,944 KB
testcase_43 AC 299 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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)
  if t[i] == "?" then
    t[i] = false
  else
    t[i] = t[i]:byte() - 96
  end
end
for i = 1, n - 1 do
  if t[i] and t[i] == t[i + 1] then
    print(0) os.exit()
  end
end
for i = 1, n - 2 do
  if t[i] and t[i] == t[i + 2] then
    print(0) os.exit()
  end
end
if n == 1 then
  print(t[1] and 1 or 26) os.exit()
end
local dp1, dp2 = {}, {}
if not t[1] and not 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 not t[1] then
  local i2 = t[2]
  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 not t[2] then
  local i1 = t[1]
  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]
  local i2 = t[2]
  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
  if t[i] then
    local z = t[i]
    for prvprv = 1, 26 do
      if prvprv ~= z then
        for prv = 1, 26 do
          if prv ~= z then
            local d = (prv - 1) * 26 + z
            dst[d] = dst[d] + src[(prvprv - 1) * 26 + prv]
          end
        end
      end
    end
  else
    local row = {}
    local col = {}
    for j = 1, 26 do row[j], col[j] = 0, 0 end
    local all = 0
    for j = 1, 26 do
      for k = 1, 26 do
        local sz = src[(j - 1) * 26 + k]
        all = all + sz
        row[j] = row[j] + sz
        col[k] = col[k] + sz
      end
    end
    local z = t[i]
    for j = 1, 26 do
      for prv = 1, 26 do
        if prv ~= j then
          local v = col[prv] - src[(j - 1) * 26 + prv]
          local d = (prv - 1) * 26 + j
          dst[d] = v
        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