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?

More than 1 year has passed since last update.

【AtCoder】Fortranを用いたABC297C - PC on the Tableの解説

Last updated at Posted at 2023-04-10

背景

  • AtCoder Beginner Contest 297に参加しました。
  • 自身のプログラミング技術の向上と、Fortranの発展を願って投稿します。
    (Fortran使ってる人が6000人中2人でした。かなしい。)
  • A問題B問題はこちら。(記事が長くなるので分けました。)

実行環境

  • Macbook(M1)
  • Docker
  • VsCode
  • Fortran(gcc version 10.2.1)

問題内容

問題文はこちらです。
いろいろ書いてありますが、内容は次のとおりです。

長さWの文字列SがH個あります。

S_1 ,S_2 ,S_3 ... S_H

文字列$S_i$の中身には、.もしくはTが合計でW文字分格納されています。

$S_i$の隣り合う文字がTとTの場合、j番目をPj+1番目をCに書き換えます。
つまり、TTPCへ書き換えます。

この書き換えを$S_1$から$S_H$まで繰り返し、結果を出力します。

解説ポイント

基本的な考え方は、公式の解説(C++)の通りとなります。

以下はFortranでのポイントと注意点です。

  • 隣り合う文字がTとTになっているかどうか、do文を用いて開始位置jをずらしながら調べます。
  • 結果は左詰めで出力します。従って、書式を指定する必要があります。

サンプルコード

program ABC297b
    implicit none
    integer(8) i, j, k, K1
    integer(8) B2(2), R2(2)
    character(8):: S

    j = 1
    k = 1
    !write (*, *) '>> S'
    read (*, *) S

    do i = 1, 8
        if (S(i:i) == 'B') then
            B2(j) = mod(i, 2)
            j = j + 1
        else if (S(i:i) == 'K') then
            K1 = i
        end if
        if (S(i:i) == 'R') then
            R2(k) = i
            k = k + 1
        end if
    end do

    !print *, 'R=', R2; print *, 'B=', B2; print *, 'K=', K
    if (R2(1) < K1 .and. K1 < R2(2) .and. B2(1) .ne. B2(2)) then
        write (*, "(a)") 'Yes'
    else
        write (*, "(a)") 'No'
    end if
end program

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?