본문 바로가기
Algorithm/BAEKJOON

[BAEKJOON] 단계별로 풀어보기 6. 문자열

by noey_hus 2022. 10. 1.

https://www.acmicpc.net/step/7

 

문자열 단계

정수를 문자열로 입력받는 문제. Python처럼 정수 크기에 제한이 없다면 상관 없으나, 예제 3은 일반적인 정수 자료형에 담기에 너무 크다는 점에 주목합시다.

www.acmicpc.net

# 11654 : 아스키 코드

a = input()
print(ord(a))
  • ord()
    • 문자에 맞는 아스키코드(숫자값) 출력
  • chr()
    • 아스키코드에 해당하는 문자 출력

# 11720 : 숫자의 합

n = input()
result = 0
num_list = input()
for i in num_list:
  result += int(i)
print(result)

# 10809 : 알파벳 찾기

s = input()
abc_list = [chr(i) for i in range(97, 123)]
result_list = []

for i in abc_list:
  result_list.append(s.find(i))

print(' '.join(map(str, result_list)))
  • find()
    • 문자열에서 특정 문자를 찾아 그 문자의 인덱스를 출력하는 함수
    • 값이 존재하지 않으면 -1를 출력
  • ''.join()
    • list를 하나의 string으로 변환
    • () 안의 list의 값들을 '' 안의 문자로 연결시켜줌

# 2675 : 문자열 반복

t = int(input())

for i in range(t):
  n, s = input().split()
  result = []
  for j in s:
    result.append(j*int(n))
  print(''.join(result))

# 1157 : 단어 공부

s = input().upper()
s_set = list(set(i for i in s))
max_list = []
max_num = 0

for i in s_set:
  if s.count(i) > max_num:
    max_num = s.count(i)
    max_list = [i]
  elif s.count(i) == max_num:
    max_list.append(i)

if len(max_list) == 1:
  print(max_list[0])
else:
  print('?')

# 1152 : 단어의 개수

print(len(input().split()))

# 2908 : 상수

a, b = map(str, input().split())
a = int(a[::-1])
b = int(b[::-1])
print(max(a, b))
# 문자열 뒤집기
# 1 : reverse 이용
a = 'abcde'
a = list(a) # ['a', 'b', 'c', 'd', 'e']
a.reverse() # ['e', 'd', 'c', 'b', 'a']
a = ''.join(a) # edcba

# 2 : [::-1] 이용
b = 'abcde'
b[::-1] # edcba

# 5622 : 다이얼

abc_list = [chr(i) for i in range(65, 91)]
num_list = [2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9]
dial = {s : num_list[i] for i, s in enumerate(abc_list)}
dial_list = [dial[i] for i in input()]

result = 0
for i in dial_list:
  result += (i+1)

print(result)

# 2941 : 크로아티아 알파벳

croatia_alphabet = {'c=':'*', 'c-':'*', 'dz=':'*', 'd-':'*', 'lj':'*', 'nj':'*', 's=':'*', 'z=':'*'}
s = input()

for i in croatia_alphabet.keys():
  s = s.replace(i, croatia_alphabet[i])

print(len(s))
  • .replace(old, new, [count])
    • 현재 문자열에서 old 부분을 new로 변경
    • 만약 현재 문자열에서 old가 여러번 등장한다면 앞에서부터 count의 개수만큼의 old만 new로 변경

# 1316 : 그룹 단어 체커

n = int(input())
word_count = n

for i in range(n):
  word = input()
  w_set = set(word[0])
  for j in range(1, len(word)):
    if word[j] == word[j-1]:
      pass
    else:
      if word[j] in w_set:
        word_count -= 1
        break
      else:
        w_set.add(word[j])

print(word_count)
  1. word의 문자열 차례대로 이전의 문자열과 같은지 확인
    • 만약 같다면 pass
  2. 같지 않다면 해당 문자열이 이전에 등장했었는지를 확인
    • 등장했다면 그룹 단어 x
    • 등장하지 않았다면 그룹 단어