結果

問題 No.1909 Detect from Substrings
ユーザー 👑 obakyanobakyan
提出日時 2022-04-23 16:22:19
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,359 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 5,392 KB
実行使用メモリ 5,648 KB
最終ジャッジ日時 2023-09-07 09:10:58
合計ジャッジ時間 2,815 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 9 ms
4,380 KB
testcase_05 AC 7 ms
4,380 KB
testcase_06 AC 20 ms
4,376 KB
testcase_07 AC 20 ms
4,376 KB
testcase_08 AC 8 ms
5,624 KB
testcase_09 AC 6 ms
5,596 KB
testcase_10 AC 22 ms
5,004 KB
testcase_11 AC 39 ms
5,212 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 8 ms
5,648 KB
testcase_14 AC 8 ms
5,628 KB
testcase_15 AC 9 ms
5,620 KB
testcase_16 AC 9 ms
4,380 KB
testcase_17 AC 7 ms
4,376 KB
testcase_18 AC 12 ms
4,376 KB
testcase_19 AC 23 ms
4,380 KB
testcase_20 AC 23 ms
4,380 KB
testcase_21 AC 9 ms
5,576 KB
testcase_22 AC 8 ms
5,632 KB
testcase_23 AC 10 ms
4,916 KB
testcase_24 AC 6 ms
4,380 KB
testcase_25 AC 24 ms
4,728 KB
testcase_26 AC 8 ms
5,596 KB
testcase_27 AC 9 ms
5,628 KB
testcase_28 AC 9 ms
5,584 KB
testcase_29 AC 10 ms
5,648 KB
testcase_30 AC 10 ms
5,644 KB
testcase_31 AC 10 ms
5,580 KB
testcase_32 AC 11 ms
5,648 KB
testcase_33 AC 10 ms
5,648 KB
testcase_34 AC 10 ms
5,584 KB
testcase_35 AC 10 ms
5,632 KB
testcase_36 AC 11 ms
5,648 KB
testcase_37 AC 10 ms
5,644 KB
testcase_38 AC 10 ms
5,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, m = io.read("*n", "*n", "*l")
local s = {}
for i = 1, n do
  s[i] = io.read()
end
local function check(w)
  for i = 1, n do
    local skipped = false
    local si = s[i]
    for j = 1, m + 1 do
      local z = skipped and si:byte(j - 1) or si:byte(j)
      if w:byte(j) ~= z then
        if skipped then 
          return false
        else
          skipped = true
        end
      end
    end
  end
  return true
end
local c = 0
if n == 2 then
  for i = 1, m do
    if s[1]:byte(i) ~= s[2]:byte(i) then
      local w = s[1]:sub(1, i) .. s[2]:sub(i, m)
      if check(w) then
        c = c + 1
      end
      w = s[2]:sub(1, i) .. s[1]:sub(i, m)
      if check(w) then
        c = c + 1
      end
      break
    end
  end
else
  for i = 1, m do
    local t = {}
    local z = 0
    for j = 1, n do
      local b = s[j]:byte(i)
      if not t[b] then
        z = z + 1
        t[b] = {j}
      else
        table.insert(t[b], 0)
      end
    end
    if 2 < z then break end
    if z == 2 then
      local k1, v1 = next(t)
      local k2, v2 = next(t, k1)
      if #v1 == 1 then
        local w = s[t[k2][1]]:sub(1, i) .. s[t[k1][1]]:sub(i, m)
        if check(w) then c = c + 1 end
      else
        local w = s[t[k1][1]]:sub(1, i) .. s[t[k2][1]]:sub(i, m)
        if check(w) then c = c + 1 end
      end
      break
    end
  end
end
print(c)
0