![[백준 / BOJ] 10809번 알파벳 찾기 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FOnSDZ%2FbtrJln5FKSz%2FfAlzfod0nZKo2WZIfOLaok%2Fimg.png)
[백준 / BOJ] 10809번 알파벳 찾기 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2022. 1. 17. 09:22
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/10809
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출
www.acmicpc.net
문제

문제 풀이
여러 가지 아이디어가 있지만 크기가 26인 배열을 선언해 해결하는 것이 가장 간단해 보인다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
do
{
cin.get(c);
i++;
if (alp[c - 97] != -1)
continue;
alp[c - 97] = i;
} while (c != '\n');
알파벳이 26개이므로 배열의 크기는 26으로 설정했다.
다시금 cin / cin.get / cin.getline 의 특성을 생각해보자.
- cin은 엔터를 누를 시 버퍼에 개행문자가 남아있어 어떠한 문제가 생길 수 있다.
- cin.get은 문자만 입력받을 수 있고, \n을 다음 입력 버퍼에서 소거해준다. (엔터도 입력받을 문자로 간주)
- cin.getline은 문자열을 입력받을 수 있다.
따라서 위의 알고리즘에서는 while문 조건에 c != "\n"을 넣었으므로, 개행문자를 무시해야 해 cin.get을 사용했다.
...인데, 포스트를 전체적으로 수정하는 과정에서 너무 알고리즘을 못 짠것 같아 다시 코딩해보았다.
#include <iostream>
#include <string>
void init() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
int main() {
init();
std::string text;
std::string alp = "abcdefghijklmnopqrstuvwxyz";
std::cin >> text;
for(int i = 0; i < alp.length(); ++i)
std::cout << (int)text.find(alp[i]) << " ";
return 0;
}
STL의 find 함수는 찾는 값이 존재한다면 해당 값의 인덱스를 출력한다. 또한 (int)를 붙인 이유는 만약 찾을 값이 존재하지 않는다면 -1을 반환하는데, 이 값이 unsigned로 되어있어 이상한 양수가 출력되기 때문이다.
Python 상세 풀이
Python 풀이 펼쳐보기
alp = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -
1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
for i in range(len(val)):
if alp[ord(val[i]) - 97] != -1:
continue
alp[ord(val[i]) - 97] = i
alp 리스트에 26개의 -1을 미리 선언해둔 후, 입력받은 글자 수만큼 for문을 도는 동안 처음 인덱스를 alp에 저장한다.
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
int alp[26]; | |
char c; | |
int i = -1; | |
for (int k = 0; k < 26; k++) | |
{ | |
alp[k] = -1; | |
} | |
do | |
{ | |
cin.get(c); | |
i++; | |
if (alp[c - 97] != -1) | |
continue; | |
alp[c - 97] = i; | |
} while (c != '\n'); | |
for (i = 0; i < 26; i++) | |
{ | |
cout << alp[i] << " "; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
void init() { | |
std::ios_base::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
std::cout.tie(nullptr); | |
} | |
int main() { | |
init(); | |
std::string text; | |
std::string alp = "abcdefghijklmnopqrstuvwxyz"; | |
std::cin >> text; | |
for(int i = 0; i < alp.length(); ++i) | |
std::cout << (int)text.find(alp[i]) << " "; | |
return 0; | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val = input() | |
alp = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - | |
1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] | |
for i in range(len(val)): | |
if alp[ord(val[i]) - 97] != -1: | |
continue | |
alp[ord(val[i]) - 97] = i | |
for i in range(26): | |
print(f"{alp[i]} ", end='') |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 1157번 단어 공부 (C++, Python) (0) | 2022.01.17 |
---|---|
[백준 / BOJ] 2675번 문자열 반복 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 11720번 숫자의 합 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 11654번 아스키 코드 (C++, Python) (0) | 2022.01.17 |
[백준 / BOJ] 1065번 한수 (C++, Python) (0) | 2021.09.07 |
@Reo :: 코드 아카이브
자기계발 블로그