※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 9771번 문제인 Word Searching이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/9771
9771번: Word Searching
The first line contains a single word to search. A word contains at most 20 characters. The next line and the rest is a text to search for that word. The text can contain up to 100 lines including blank lines. No line exceeds 250 characters. A word cannot
www.acmicpc.net
주어지는 문자열에서 목표하는 단어와 일치하는 문자열이 몇 번 등장하는지를 세는 문제이다.
substr을 이용해 문제를 간단히 해결하자. 또한, 굳이 줄 단위로 입력을 받지 않아도 각 줄을 구성하는 단어단위로 입력을 받아 목표하는 단어를 찾아도 상관 없으므로 단어 단위로 입력을 받아도 좋다. (단어는 공백을 포함하지 않을 것임을 관찰하자.)
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
string target, cur;
int tlen, clen;
int ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> target;
tlen = target.length();
while (cin >> cur) {
clen = cur.length();
for (int i = tlen; i <= clen; i++) {
if (cur.substr(i - tlen, tlen) == target) ans++;
}
}
cout << ans;
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 27465 // C++] 소수가 아닌 수 (0) | 2023.02.14 |
---|---|
[BOJ 2346 // C++] 풍선 터뜨리기 (0) | 2023.02.14 |
[BOJ 2033 // C++] 반올림 (0) | 2023.02.13 |
[BOJ 27451 // C++] 마키마씨가 정해주는 오늘 점심의 맛 (0) | 2023.02.13 |
[BOJ 9770 // C++] GCD (0) | 2023.02.13 |