일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준
- 2557
- scanf
- 구조체와 클래스의 공통점 및 차이점
- 입/출력
- 알고리즘 공부방법
- 엑셀
- getline
- c++
- k-eta
- correlation coefficient
- Django란
- EOF
- double ended queue
- Django의 편의성
- 표준 입출력
- 매크로
- vscode
- Django Nodejs 차이점
- iOS14
- UI한글변경
- 프레임워크와 라이브러리의 차이
- string 함수
- 입출력 패턴
- 자료구조
- string 메소드
- 이분그래프
- 시간복잡도
- 연결요소
- 장고란
Archives
- Today
- Total
Storage Gonie
(2) [C++, Java] 백준 No.10809 : 알파벳 찾기 본문
반응형
문제
풀이
# C++
- count에 이어서 find 함수를 사용하면 쉽고 빠르다.
#include <iostream>
using namespace std;
int main(void)
{
ios::sync_with_stdio(false);
string s;
cin >> s;
int len;
len = s.size();
for (int i = 'a' ; i <= 'z' ; i++) // 'z' 도 포함시키는걸 깜빡하지 말자
{
for (int j = 0; j < len ; j++)
{
if (s[j] == i)
{
cout << j << " ";
break;
}
else if (s[j] != i && j == (len-1))
{
cout << -1 << " ";
break;
}
}
}
}
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
for (int i='a'; i<='z'; i++) {
auto it = find(s.begin(), s.end(), i);
if (it == s.end()) {
cout << -1 << ' ';
} else {
cout << (it - s.begin()) << ' ';
}
}
cout << '\n';
return 0;
}
# Java
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int[] position = new int[26];
for (int i=0; i<26; i++) {
position[i] = -1;
}
for (int i=0; i<s.length(); i++) {
int c = s.charAt(i) - 'a';
if (position[c] == -1) {
position[c] = i;
}
}
for (int i=0; i<26; i++) {
System.out.print(position[i] + " ");
}
System.out.println();
}
}
반응형
'알고리즘 > 백준풀이5. 문자열' 카테고리의 다른 글
(6) [C++, Java] 백준 No.10824 : 네 수 (0) | 2019.04.25 |
---|---|
(5) [C++, Java] 백준 No.11655 : ROT13 (0) | 2019.04.25 |
(4) [C++, Java] 백준 No.2743 : 단어 길이 재기 (0) | 2019.04.25 |
(3) [C++, Java] 백준 No.10820 : 문자열 분석 (0) | 2019.04.25 |
(1) [C++, Java] 백준 No.10808 : 알파벳 개수 (0) | 2019.04.25 |
Comments