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.

Swiftで解くAtCoder(ABC303)

Last updated at Posted at 2023-05-30

1.はじめに

この記事では、AtCoder(ABC303)のA~C問題をACしたコードの公開, 解説を行います。
Swiftを使ってコンテストに参加する方の参考になれば幸いです。

2.事前準備

標準入力を読み込むためのメソッドです。
コードの先頭に貼り付けて、適宜使用します。

import Foundation

// 標準入力から整数を読み込む
func readInt() -> Int{
    return Int(readLine()!)!
}

// 標準入力から整数列を読み込む
func readInts() -> [Int]{
    return readLine()!.split(separator:  " ").map { Int(String($0))! }
}

// 標準入力から文字列を読み込む
func readString() -> String{
    return readLine()!
}

// 標準入力から文字列配列を読み込む
func readStrings() -> [String]{
    return readLine()!.split(separator:  " ").map { String($0) }
}

3.A - Similar String

問題文や制約はこちらからご確認ください。
AtCoder ProblemsによるDifficultyは22になります。

let N = readInt()
let S = Array(readString())
let T = Array(readString())

var check = true
for i in 0..<N {
    if S[i] != T[i] {
        if S[i] == "1" && T[i] == "l" || S[i] == "l" && T[i] == "1" {
            
        }else{
            if S[i] == "0" && T[i] == "o" || S[i] == "o" && T[i] == "0" {
                
            }else{
                check = false
            }
        }
    }
}

print(check ? "Yes" : "No")

4.B - Discord

問題文や制約はこちらからご確認ください。
AtCoder ProblemsによるDifficultyは112になります。

let line1 = readInts()
let N = line1[0]
let M = line1[1]
var a = [[Int]]()
for _ in 0..<N {
    a.append(readInts())
}

var count = 0
for i in 1..<N+1 {
    for j in i+1..<N+1 {
        var check = false
        for k in 0..<M {
            if abs(a[k].firstIndex(of: i)! - a[k].firstIndex(of: j)!) == 1 {
                check = true
                break
            }
        }
        if !check {
            count += 1
        }
    }
}

print(count)

5.C - Dash

問題文や制約はこちらからご確認ください。
AtCoder ProblemsによるDifficultyは417になります。

let line1 = readInts()
let N = line1[0]
let M = line1[1]
var H = line1[2]
let K = line1[3]
let S = Array(readString())
var xy = Set<[Int]>()
for _ in 0..<M {
  xy.insert(readInts())
}

var check = true
var tx = 0
var ty = 0
for i in 0..<N {
  H -= 1
  if H < 0 {
    check = false
    break
  }
  switch S[i] {
    case "R":
      tx += 1
    case "L":
      tx -= 1
    case "U":
      ty += 1
    case "D":
      ty -= 1
    default:
      break
  }
  if H < K {
    if xy.contains([tx, ty]) {
      H = K
      xy.remove([tx, ty])
    }
  }
}

print(check ? "Yes" : "No")

6.最後に

間違いや改善点等あれば、ご指摘いただけると幸いです。
何卒よろしくお願いいたします。

0
0
1

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?