티스토리 뷰
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
문제풀이
주어지는 영어 문장에서 특정한 문자열의 개수를 찾는 문제다.
string 라이브러리의 find 함수를 쓰면 간단하게 찾을 수 있다.
size_t find(const string& str, size_T pos) const;
- str : 찾고자 하는 문자열
- pos : 검색을 시작할 위치
- 문자열을 찾았다면, 해당 문자열의 시작 위치를 리턴하고, 그렇지 않을 경우 npos 리턴
string::npos란?
size_type으로 정의된 특수값이다.
string::npos란 -1 값을 가지는 상수로 find() 함수 수행 시에 찾는 문자열이 없을 때 반환된다.
find 함수가 해당 문자열의 시작 위치를 반환해주므로 문자열을 찾았다면 그 위치에서 문자열의 크기만큼 더해준 위치에서부터 다시 해당 문자열을 탐색해나간다.
C++ 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include <string> | |
using namespace std; | |
int main(int argc, char** argv) { | |
int T = 10; | |
for (int test_case = 1; test_case <= T; ++test_case) { | |
cin >> test_case; | |
string str, word; | |
cin >> str >> word; | |
int answer = 0; | |
int pos = 0; | |
while (word.find(str, pos) != string::npos) { | |
answer++; | |
pos = word.find(str, pos) + str.size(); | |
} | |
cout << "#" << test_case << " " << answer << "\n"; | |
} | |
return 0; | |
} |
'Problem Solving > SWEA' 카테고리의 다른 글
[SWEA 1216] 회문2 C++ (0) | 2021.07.28 |
---|---|
[SWEA 1215] 회문1 C++ (0) | 2021.07.28 |
[SWEA 1220] Magnetic C++/Java (0) | 2021.07.28 |
[SWEA 1210] Ladder1 C++/Java (0) | 2021.07.28 |
[SWEA 1209] Sum C++/Java (0) | 2021.07.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 큐
- 투 포인터
- BFS
- dfs
- 배열
- Two Pointer
- 이분 탐색
- 백준
- SW Expert Academy
- C++
- 브루트포스
- Kotlin
- programmers
- 문자열
- 트리
- 자바
- 두 포인터
- 정렬
- SWEA
- 프로그래머스
- 스택
- 위상 정렬
- Java
- 알고리즘
- algorithm
- BOJ
- 분할 정복
- 구현
- 그래프
- 재귀
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 |
글 보관함