https://www.acmicpc.net/problem/1544

 

1544번: 사이클 단어

사이클 단어는 어떤 단어를 원형 모양으로 차례대로 쓴 것이다. 따라서, 어떤 단어를 이렇게 쓴 후에 임의의 단어를 고른다. 그 후에 시계방향으로 차례대로 읽으면 그 것이 단어가 된다. 만약에

www.acmicpc.net

문자열 구현 문제다.

 

풀이

주어지는 시간이 넉넉한 편이므로 모든 문자열들을 비교하여 개수를 세면 된다. 두 문자열을 매개변수로 받은 뒤 시계방향으로 모든 경우를 읽어 같은지 같은지 다른지 판별하는 isSame(a:String, b:Stirng) -> Bool 함수를 구현하여 정답을 구하였다.

 

정답 코드

import Foundation
let n = Int(readLine()!)!
var words = [String]()
var ans = [String]()
for _ in 0..<n{
words.append(readLine()!)
}
func isSame(a:String, with b:String) -> Bool {
let target = a.map{String($0)}
let temp = b.map{String($0)}
if a.count == b.count{
let end = b.count
for i in 0..<end{
var idx = i
var newWord = [String]()
for _ in 0..<end{
newWord.append(temp[idx])
idx += 1
idx %= end
}
if target == newWord{
return true
}
}
}
return false
}
ans.append(words[0])
for i in 1..<n{
let word = words[i]
var flag = true
for k in 0..<ans.count{
if (isSame(a: word, with: ans[k])){
flag = false
}
}
if flag{
ans.append(word)
}
}
print(ans.count)
view raw 1544.swift hosted with ❤ by GitHub

'Problem Solving > BOJ' 카테고리의 다른 글

[14466] 소가 길을 건너간 이유 6  (0) 2023.02.03
[14464] 소가 길을 건너간 이유 4  (0) 2023.02.01
[1263] 시간 관리  (0) 2023.01.29
[2607] 비슷한 단어  (2) 2023.01.27
[6503] 망가진 키보드  (0) 2023.01.24

+ Recent posts