0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

多重ループ問題!行番号とindexのズレに注意!

Posted at

Paizaの多重ループ問題で「m+2」を使って爆死…。
行番号と配列のインデックスのズレに気づかず…。でも解決しました!


問題概要
m個の文字 c_1, ..., c_m と n個の文字列 S_1, ..., S_n が与えられ、
各 c_i が各 S_j に含まれるかを判定して "YES" / "NO" を出力する問題。

入力例

1
a
2
paiza
kyoko

出力例

YES
NO


ミスったポイント

問題文に「(m + 2) 行目に n がある」と書いてあったから、
const n = lines[m + 2]; // インデックスのズレでエラー!
と書いたけど、動かない…。

行番号は1から始まるけど、配列のインデックスは0から!
だから m + 1 を使うのが正解だった。


解決コード

const n = Number(lines[m + 1]); // 正しく n を取得!

for (let count = 1; count <= m; count++) {
    let charC = lines[count];
    for (let i = 1; i <= n; i++) {
        let stringS = lines[m + 1 + i];
        console.log(stringS.includes(charC) ? "YES" : "NO");
    }
}

ポイント
✅ .includes() で YES / NO をスマート判定
✅ m+1 で n の取得ミスを回避
✅ 三項演算子 ? : でコードを短縮

これで僕もエラー地獄から脱出!

僕の失敗談と解決話!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?