トピック #9 文字列
AIZU ONLINE JUDGEの問題にSwiftで解答したものになります。競技プログラミングは普段やりませんので、こうしたほうが簡単!などがあればコメントで教えて下さい。
ブラウザでプログラミング・実行ができる「オンライン実行環境」paiza.IOでテスト出力が行えます
8_A 単語の検索
問題
1つの単語 W と文章 T が与えられます。T の中にある W の数を出力するプログラムを作成して下さい。文章 T に含まれるスペースまたは改行で区切られた文字列を単語 Ti とします。すべての Ti において単語 W と同じになるものを数えて下さい。なお、大文字と小文字は区別しません。
解答
let a = readLine() ?? ""
var count = 0
while(true) {
let b = readLine()!
if b == "END_OF_TEXT" {
break
}
let c = b.split(separator: " ")
for i in c {
if a == i {
count += 1
}
}
}
print(count)
8_B シャッフル
問題
1つのアルファベットが描かれた n 枚のカードの山をシャッフルします。
1回のシャッフルでは、下から h 枚のカードをまとめて取り出し、それを残ったカードの山の上に積み上げます。
カードの山は以下のように1つの文字列で与えられます。
abcdeefab
最初の文字が一番下にあるカード、最後の文字が一番上にあるカードを示しています。
例えば、これを h が 4 でシャッフルすると、最初の4文字 abcd が、残りの文字 eefab の末尾へ連結されるので以下のようになります:
eefababcd
このシャッフルを何回か繰り返します。
カードの山の最初の並び(文字列)と h の列を読み込み、最後の並び(文字列)を出力するプログラムを作成して下さい。
解答
while(true) {
var text = readLine()!
if text == "-" {
break
}
let number = Int(readLine()!)!
for i in 0 ..< number {
let j = Int(readLine()!)!
let indexStart = text.startIndex
let indexPoint = text.index(text.startIndex, offsetBy: j)
let indexEnd = text.endIndex
let shuffuleWordFront = text[indexStart ..< indexPoint]
let shuffuleWordBack = text[indexPoint ..< indexEnd]
text = String(shuffuleWordBack + shuffuleWordFront)
}
print(text)
}
8_C カードゲーム
問題
太郎と花子がカードゲームをする。二人はそれぞれn枚のカードを持っており、nターンの勝負を行う。各ターンではそれぞれ1枚ずつカードを出す。カードにはアルファベットからなる動物の名前が書かれており、辞書順で大きいものがそのターンの勝者となる。勝者には3ポイント、引き分けの場合にはそれぞれ1ポイントが加算される。
太郎と花子の手持ちのカードの情報を読み込み、ゲーム終了後のそれぞれの得点を出力するプログラムを作成せよ。
Input
一行目にカードの数nが与えられる。続くn行に各ターンのカードの情報が与えられる。1つ目の文字列が太郎のカードに書かれている文字列、2つ目の文字列が花子のカードに書かれている文字列である。
Output
1つ目の数字が太郎の得点、2つ目の数字が花子の得点として1行に出力せよ。2つの数字の間に1つの空白を出力せよ。
解答
import Foundationfunc dictionary(_ word1: String, _ word2: String) -> String {
let lowerWord1 = word1.lowercased().unicodeScalars.map({UInt32($0)})
let lowerWord2 = word2.lowercased().unicodeScalars.map({UInt32($0)})
let word1Size = lowerWord1.count
let wordSize2 = lowerWord2.count
var anserWord = String()
var index = 0
while(true) {
if index == word1Size {
anserWord = word2
break
}
if index == wordSize2 {
anserWord = word1
break
}
if lowerWord1[index] > lowerWord2[index] {
anserWord = word2
break
} else if lowerWord1[index] < lowerWord2[index] {
anserWord = word1
break
} else {
index += 1
}
}
return anserWord
}
let a = Int(readLine()!)!
var taroPoint = 0
var hanakoPoint = 0
for i in 0 ..< a {
let words = readLine()!.split(separator: " ").map({String($0)})
let word1 = words[0]
let word2 = words[1]
let winWord = dictionary(word1, word2)
if winWord == word1 && winWord != word2 {
taroPoint += 3
} else if winWord == word2 && winWord != word1 {
hanakoPoint += 3
} else {
taroPoint += 1
hanakoPoint += 1
}
}
print(taroPoint, hanakoPoint)
8_D 文字列変換
問題
長いので省略。詳しくはこちら
解答
import Foundation
func rev(_ str: String,_ s: Int, _ e: Int) -> String {
let startIndex = str.index(str.startIndex, offsetBy: s)
let endIndex = str.index(str.startIndex, offsetBy: e)
let repStr = str[startIndex..<endIndex].reversed()
let reStr = str[str.startIndex..<startIndex] + String(repStr) + str[endIndex..<str.endIndex]
return String(reStr)
}
func rep(_ str: String,_ repStr: String,_ s: Int, _ e: Int) -> String {
var str = str
let start = str.index(str.startIndex, offsetBy: s)
let end = str.index(str.startIndex, offsetBy: e)
let replacedStr = str[start...end]
str = str.replacingOccurrences(of: replacedStr, with: repStr)
return str
}
func pri(_ str: String, _ s: Int, _ e: Int) -> Void {
let start = str.index(str.startIndex, offsetBy: s)
let end = str.index(str.startIndex, offsetBy: e)
let priStr = str[start...end]
print(priStr)
}
var text = readLine()!
let n = Int(readLine()!)!
for i in 0 ..< n {
let a = readLine()!.split(separator: " ")
switch a[0] {
case "print":
pri(text, Int(a[1])!, Int(a[2])!)
case "replace":
text = rep(text, String(a[3]), Int(a[1])!, Int(a[2])!)
case "reverse":
text = rev(text, Int(a[1])!, Int(a[2])!+1)
default:
break
}
}