Skip to content
CatBus

Tag: string

All the articles with the tag "string".

BOJ16719GOLD 5
sys.setrecursionlimit(10**6)

string = sys.stdin.readline().strip()
length = len(string)

visited = [False] * length

def select_char(start, end):
    if start > end:
        return

    min_char = 'Z' + '1' 
    min_idx = -1
    for i in range(start, end + 1):
        if string[i] < min_char:
            min_char = string[i]
            min_idx = i

    visited[min_idx] = True

    current_result = ""
    for i in range(length):
        if visited[i]:
            current_result += string[i]
    print(current_result)
    select_char(min_idx + 1, end)
    select_char(start, min_idx - 1)

select_char(0, length - 1)

ZOAC

백준 16719번 'ZOAC' (골드 5) 문제 풀이. implementation, string, recursion 로 접근했다.

2025.08.31·2분·implementation
BOJ1652SILVER 5
import re

N = int(input())
room = [input() for _ in range(N)]

# 가로
row_cnt = sum(len(re.findall(r'\.{2,}', row)) for row in room)

# 세로
transposed = [''.join(row[i] for row in room) for i in range(N)]
col_cnt = sum(len(re.findall(r'\.{2,}', col)) for col in transposed)

print(row_cnt, col_cnt)

정규표현식 \.{2,}는 “연속된 2개 이상의 .”을 의미한다.

누울 자리를 찾아라

백준 1652번 '누울 자리를 찾아라' (실버 5) 문제 풀이. implementation, string 로 접근했다.

2025.04.18·7분·implementation
BOJ1157BRONZE 1
word = input().upper()

counter = defaultdict(int)

max_cnt = 0
max_alpha = ''
same_chk = False

for a in word:
    counter[a] += 1
    if counter[a] > max_cnt:
        max_cnt = counter[a]
        max_alpha = a
        same_chk = False
    elif counter[a] == max_cnt:
        same_chk = True

if same_chk:
    print('?')
else:
    print(max_alpha)

단어 공부

백준 1157번 '단어 공부' (브론즈 1) 문제 풀이. implementation, string 로 접근했다.

2023.12.05·2분·implementation
BOJ1541SILVER 2

예를 들면 55-50+40+90-100+10-11 이라는 수식이 입력됐을 때 55-(50+40+90)-(100+10)-11 = 55-50-40-90-100-10-11 로 만들 수 있다.

st = input().strip()+ '+'
total = 0
minus = False
num = ''
for c in st:
    if c.isdigit():
        num += c
    else:
        if minus:
            total -= int(num)
        else:
            total += int(num)
        if c == '-':
            minus = True
            
        num = ''
print(total)

입력은 숫자와 부호가 섞인 문자열로 주어지기 때문에 그 문자열을 확인하면서 문자가 숫자인지 부호인지에 때라 다르게 처리를 해주어야 한다.

잃어버린 괄호

백준 1541번 '잃어버린 괄호' (실버 2) 문제 풀이. math, string, greedy algorithm 로 접근했다.

2022.12.12·3분·math
BOJ1181SILVER 5
print = sys.stdout.write

n = int(input())
len_cnt = [set() for i in range(51)]
for _ in range(n):
    word = input().strip()
    len_cnt[len(word)].add(word)

for words in len_cnt:
    if words:
        if len(words) == 1:
            print(f"{list(words)[0]}\n")
        else:
            for w in sorted(list(words)):
                print(f"{w}\n")

단어 정렬

백준 1181번 '단어 정렬' (실버 5) 문제 풀이. string, sort 로 접근했다.

2022.10.13·2분·string
BOJ1427SILVER 5
num = input().strip()
cnt_dict = {str(n):0 for n in range(9, -1, -1)}
for i in num:
    cnt_dict[i] += 1
ans = ''
for i in cnt_dict:
    for _ in range(cnt_dict[i]):
        ans += i

print(ans)

9~0까지의 수를 key로 가지고 value가 0인 dictionary를 만들고 수가 얼마나 나왔는지 count 한다. 이후 dictionary의 key 순서대로(내림차순) count 된 수만큼 문자를 붙여가며 답을 완성한다.

소트 인사이드

백준 1427번 '소트 인사이드' (실버 5) 문제 풀이. string, sort 로 접근했다.

2022.10.13·1분·string
BOJ10610SILVER 5
nums = str(sys.stdin.readline().strip())
if '0' not in nums:
    print(-1)
else:
    l = [0] * (int(max(nums)) + 1)
    s = ''
    sum = 0
    for i in nums:
        l[int(i)] += 1
    for i in range(len(l)-1, 0, -1):
        s += str(i) * l[i]
        sum += i * l[i]
    if sum % 3 == 0: print(int(s) * (10 ** l[0]))
    else: print(-1)

30

백준 10610번 '30' (실버 5) 문제 풀이. math, string, greedy algorithm 로 접근했다.

2022.02.04·3분·math