본문 바로가기
백준 알고리즘

[백준][C++] 10809번 : 알파벳 찾기

by 탱글한푸딩 2023. 7. 28.
반응형

문제

https://www.acmicpc.net/problem/10809

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출

www.acmicpc.net


코드

#1

#include <iostream>
using namespace std;

int main() 
{
    string s;
    string alp = "abcdefghijklmnopqrstuvwxyz";
    int i, j;

    cin >> s;

    for (i = 0; i < alp.length(); i++)
    {
        for (j = 0; j < s.length(); j++)
        {
            if (s[j] == alp[i])
            {
                cout << j << " ";
                break;
            }
        }

        if (s[j] != alp[i])
        {
            cout << -1 << " ";
        }
    }

    return 0;
}

#2

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	string alp = "abcdefghijklmnopqrstuvwxyz";	// 알파벳 배열 선언이 필요

	cin >> s;
	
	// string str = "문자열";
	// str.find(원하는 문자 혹은 문자열,startIndex);
	// str에서 원하는 문자가 있을때는 str에 해당되는 위치를 반환하고 없으면 -1을 리턴해주는 구조
	for (int i = 0; i < alp.length(); i++)
	{
		cout << (int)s.find(alp[i]) << " ";
	}

	return 0;
}
반응형