BOJ : https://www.acmicpc.net/problem/10809
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출력한다. 단어의 첫 번째 글자는 0번째 위치이고, 두 번째 글자는 1번째 위치이다.
www.acmicpc.net
아스키코드로 문제를 보면
소문자는 'a' 가 기본값이 되고
소문자 - 'a' 를 하면 0이 됨을 이용하여 문제를 해결한다.
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#include <cstdio>
#include <string.h>
#pragma warning(disable:4996)
char str[100];
int arr[27];
int main() {
scanf("%s", str);
for (int i = 1; i <= 26; i++) arr[i] = -1;
for (int i = 0; i < strlen(str); i++) {
if (arr[str[i] - 'a' + 1] == -1) arr[str[i] - 'a' + 1] = i;
}
for (int i = 1; i <= 26; i++) printf("%d ", arr[i]);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 11655번 ROT13 (0) | 2020.01.12 |
---|---|
BOJ 10820번 문자열 분석 (0) | 2020.01.12 |
BOJ 10845 큐 (0) | 2020.01.11 |
BOJ 10799번 쇠막대기 (0) | 2020.01.11 |
BOJ 9012번 괄호 (0) | 2020.01.11 |