BOJ 10809 알파벳 찾기

1 개요[ | ]

BOJ 10809 알파벳 찾기
  • 한 단어에서 각 알파벳이 처음 등장하는 위치를 찾아봅니다
  • 알고리즘 분류: 문자열 처리

2 C++[ | ]

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

int main() {
    string S;
    cin >> S;
    int start[26] = {-1};
    for(int i=0; i<26; i++) {
        start[i] = S.find('a'+i);
    }
    for(int i=0; i<26; i++) {
        cout << start[i] << ' ';
    }
    cout << '\n';
}

3 Java[ | ]

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String word = sc.next();
        
        // init
        int[] positions = new int[26];
        for(int i=0; i<positions.length; i++) {
            positions[i] = -1;
        }
        
        // record position
        for(int i=0; i<word.length(); i++) {
            int k = word.charAt(i) - 'a';
            if( positions[k] != -1 ) continue;
            positions[k] = i;
        }
        
        // show result
        for(int i=0; i<positions.length; i++) {
            System.out.format("%d ", positions[i] );
        }
    }
}

4 Perl[ | ]

$n = <>;
for (a..z) {
    $pos = $_;
    ($loc_num, $flag) = (0) x 2;
    for (0..(length $n)-1) {
        if (substr($n, $_, 1) eq $pos) {
            $flag = 1;
            $loc_num = $_;
            last;
        }
    }
    printf("%s ", ($flag eq 1 ? $loc_num : -1));
}

5 PHP[ | ]

<?php
$str = rtrim(fgets(STDIN));
foreach(range('a','z') as $alpha) {
    $pos = strpos($str,$alpha);
    if($pos === false) $pos = -1;
	echo $pos . ' ';
}

6 Python[ | ]

s = input()
for i in range(0,26):
    print( s.find(chr(97+i)), end=' ' )
s = input()
for code in range(ord('a'),ord('z')+1):
    print( s.find(chr(code)), end=' ' )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}