Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Week3] 강동호: 파일 정리, 위장, 서로 다른 부분 문자열의 개수, 문자열 잘라내기, 빈도 정렬 #14

Merged
merged 7 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/3week/dongho/문자열 잘라내기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package `3week`.dongho

import java.io.*

fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val (r, c) = br.readLine().split(" ").map { it.toInt() }

val set = mutableSetOf<String>()
var answer = 0

val strArr = Array(r) { br.readLine() }
val columns = Array(c) { i ->
val sb = StringBuffer()
repeat(r - 1) { j ->
sb.append(strArr[j + 1][i])
}
sb.toString()
}

repeat(r) { i ->
set.clear()
repeat(c) { j ->
val str = columns[j].substring(i)
if (set.contains(str)) {
println(answer)
return
} else {
set.add(str)
}
}
answer++
}
}
31 changes: 31 additions & 0 deletions src/3week/dongho/빈도 정렬.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package `3week`.dongho

import java.io.*
import kotlin.math.min

fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
// 키에 맞는 빈도렁
val counter = mutableMapOf<Int, Int>()
// 최소 인덱스
val indexes = mutableMapOf<Int, Int>()
br.readLine()
val list = br.readLine().split(" ").mapIndexed { index, s ->
val key = s.toInt()
counter.set(key, counter.getOrDefault(key, 0) + 1)
indexes.set(key, min(indexes.getOrDefault(key, Int.MAX_VALUE), index))
key
}
println(list.sortedWith { a, b ->
// thenBy 쓰고 싶다
val aSize = counter.get(a)!!
val bSize = counter.get(b)!!
val diff = bSize - aSize
if (diff == 0) {
// 갯수 같을때는 인덱스 비교
indexes.get(a)!! - indexes.get(b)!!
} else {
diff
}
}.joinToString(" "))
}
16 changes: 16 additions & 0 deletions src/3week/dongho/서로 다른 부분 문자열의 개수.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package `3week`.dongho

import java.io.*

fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val str = br.readLine()
val set = mutableSetOf<String>()
val length = str.length
repeat(length) { i ->
IntRange(i + 1, length).forEach { j ->
set.add(str.substring(i until j))
}
}
println(set.size)
}
35 changes: 35 additions & 0 deletions src/3week/dongho/신고 결과 받기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package `3week`.dongho

import java.util.*

class `신고 결과 받기` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
fun solution(id_list: Array<String>, report: Array<String>, k: Int): IntArray {
val answerMap = id_list.associate { it to 0 }.toMutableMap()
val reportPairList = report.toSet().map {
val list = it.split(" ")
list.last() to list.first()
}.groupBy({ it.first }, { it.second })
reportPairList.forEach { to, fromList ->
if (fromList.size >= k) {
fromList.forEach { from ->
answerMap.set(from, answerMap.getOrDefault(from, 0) + 1)
}
}
}
return answerMap.map { it.value }.toIntArray()
}
}
}

fun main() {
val solution = `신고 결과 받기`.getSolution()
println(Arrays.toString(solution.solution(arrayOf("muzi", "frodo", "apeach", "neo"), arrayOf("muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "apeach muzi"), 2)))
println(Arrays.toString(solution.solution(arrayOf("con", "ryan"), arrayOf("ryan con", "ryan con", "ryan con", "ryan con"), 3)))
}
38 changes: 38 additions & 0 deletions src/3week/dongho/위장.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package `3week`.dongho

class `위장` {
companion object {
fun getSolution(): Solution {
return Solution()
}
}

class Solution {
fun solution(clothes: Array<Array<String>>): Int {
val map = mutableMapOf<String, MutableSet<String>>()
// 타입별로 그룹핑
clothes.forEach { (cloth, type) ->
val typeOfSet: MutableSet<String> = map.getOrDefault(type, mutableSetOf())
typeOfSet.add(cloth)
map.set(type, typeOfSet)
}

var answer: Int = 1
// 가능한 갯수에 안입는다는 선택지를 포함한 갯수의 곱
map.forEach { _, clothes ->
val temp = clothes.size + 1
answer *= temp
}
// 전체 종류 모자 2 / 안경 1
// 모자 갯수 + 모자없음 = 3
// 안경 갯수 + 안경없음 = 2
// 갯수들의 곱에 완전히 안입기는 빼기 1
return answer - 1
}
}
}

fun main() {
val solution = `위장`.getSolution()
println(solution.solution(arrayOf(arrayOf("yellowhat", "headgear"), arrayOf("bluesunglasses", "eyewear"), arrayOf("green_turban", "headgear"))))
}
17 changes: 17 additions & 0 deletions src/3week/dongho/파일 정리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package `3week`.dongho

import java.io.*

fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val N = br.readLine().toInt()
val map = mutableMapOf<String, Int>()
repeat(N) {
val (_, ext) = br.readLine().split(".")
map.set(ext, map.getOrDefault(ext, 0) + 1)
}
val bw = BufferedWriter(OutputStreamWriter(System.out))
map.forEach { (ext, count) -> bw.write("$ext $count\n") }
bw.flush()
bw.close()
}