알고리즘 문제
[백준] 10809번 알파벳 찾기
feelcoding
2020. 1. 26. 02:48
728x90
단계별로 풀어보기 문자열의 3단계 문제
10809번: 알파벳 찾기
각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출력한다. 단어의 첫 번째 글자는 0번째 위치이고, 두 번째 글자는 1번째 위치이다.
www.acmicpc.net
c++의 <string>을 포함하고 <string>의 편리한 함수를 사용하면 쉽게 풀 수 있다.
find 함수의 사용법은 다음과 같다.
string s1("Welcome");
cout << s1.find('o') << endl; // 4 출력 (처음부터 찾음)
cout << s1.find('o', 6) << endl; // 9 출력. (인덱스 6부터 'o'를 찾음)
if (s1.find("el", 3) == string::npos)
cout << "el은 없습니다" << endl;
find 함수는 찾고자 하는 문자 또는 문자열이 없으면 string::npos를 반환한다.
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
int alphabet[26];
for (int i = 0; i < 26; i++) {
alphabet[i] = -1;
}
alphabet[0] = s.find('a');
alphabet[1] = s.find('b');
alphabet[2] = s.find('c');
alphabet[3] = s.find('d');
alphabet[4] = s.find('e');
alphabet[5] = s.find('f');
alphabet[6] = s.find('g');
alphabet[7] = s.find('h');
alphabet[8] = s.find('i');
alphabet[9] = s.find('j');
alphabet[10] = s.find('k');
alphabet[11] = s.find('l');
alphabet[12] = s.find('m');
alphabet[13] = s.find('n');
alphabet[14] = s.find('o');
alphabet[15] = s.find('p');
alphabet[16] = s.find('q');
alphabet[17] = s.find('r');
alphabet[18] = s.find('s');
alphabet[19] = s.find('t');
alphabet[20] = s.find('u');
alphabet[21] = s.find('v');
alphabet[22] = s.find('w');
alphabet[23] = s.find('x');
alphabet[24] = s.find('y');
alphabet[25] = s.find('z');
for (int i = 0; i < 26; i++) {
cout << alphabet[i] << " ";
}
return 0;
}
728x90