https://www.acmicpc.net/problem/2776
1
5
4 1 5 2 3
5
1 3 7 9 5
1
1
0
0
1
let input = Int(readLine()!)!
var result = [Int]()
for _ in 0..<input {
let numberOfNBook1 = Int(readLine()!)!
let arrOfBook1 = readLine()!.split(separator: " ").map{Int($0)!}
let numberOfNBook2 = Int(readLine()!)!
let arrOfBook2 = readLine()!.split(separator: " ").map{Int($0)!}
for j in arrOfBook2{
if arrOfBook1.contains(j) {
print(1)
}else {
print(0)
}
}
}
print(result)
let input = Int(readLine()!)!
for _ in 0..<input {
var hash = [Int : [Int]]()
let _ = Int(readLine()!)!
let arrOfBook1 = readLine()!.split(separator: " ").map{Int($0)!}.sorted()
let _ = Int(readLine()!)!
let arrOfBook2 = readLine()!.split(separator: " ").map{Int($0)!}
for i in arrOfBook2 {
if binaraySearch(arrOfBook1, i) != -1 {
print(1)
}else {
print(0)
}
}
}
func binaraySearch(_ arr: [Int], _ value: Int ) -> Int{
var left = 0
var right = arr.count - 1
while left <= right {
let mid = left + (right - left)/2
if value == arr[mid] {
return mid
}else if value > arr[mid] {
left = mid + 1
}else {
right = mid - 1
}
}
return -1
}
import Foundation
final class FileIO {
private let buffer: Data
private var index: Int = 0
init(fileHandle: FileHandle = FileHandle.standardInput) {
self.buffer = try! fileHandle.readToEnd()! // 인덱스 범위 넘어가는 것 방지
}
@inline(__always) private func read() -> UInt8 {
defer {
index += 1
}
guard index < buffer.count else { return 0 }
return buffer[index]
}
@inline(__always) func readInt() -> Int {
var sum = 0
var now = read()
var isPositive = true
while now == 10
|| now == 32 { now = read() } // 공백과 줄바꿈 무시
if now == 45 { isPositive.toggle(); now = read() } // 음수 처리
while now >= 48, now <= 57 {
sum = sum * 10 + Int(now-48)
now = read()
}
return sum * (isPositive ? 1:-1)
}
@inline(__always) func readString() -> String {
var str = ""
var now = read()
while now == 10
|| now == 32 { now = read() } // 공백과 줄바꿈 무시
while now != 10
&& now != 32 && now != 0 {
str += String(bytes: [now], encoding: .ascii)!
now = read()
}
return str
}
}
let fIO = FileIO()
let testCases = fIO.readInt()
for _ in 0..<testCases {
var hash = [Int: Bool]()
let n = fIO.readInt()
for _ in 0..<n {
let number = fIO.readInt()
hash[number] = true
}
let m = fIO.readInt()
for _ in 0..<m {
let a = fIO.readInt()
if hash[a] == true {
print(1)
} else {
print(0)
}
}
}
let n = Int(readLine()!)!
for _ in 0..<n {
var answer = [String]()
let _ = readLine()!
let arr = Set(readLine()!.split(separator: " ").map { Int($0)! })
let _ = readLine()!
readLine()!.split(separator: " ").forEach { char in
arr.contains(Int(char)!) ? answer.append("1") : answer.append("0")
}
print(answer.joined(separator: "\n"))
}
시간복잡도 무쳤다..!!
어떻게 저렇게 생각할 수 있는지...(부럽다...)