Memo
index를 이용하여 풀 수도 있지만, 최근에 학습한 STL을 활용하기로 함
haystack의 begin에 대한 random_access_iterator를 잡고, heystack의 end에 대한 random_access_iterator를 잡음
두 범위를 기반으로 needle을 std::search로 찾아내고, 찾았을 시에는 count 증가와 begin의 위치를 needle의 길이만큼 증가시킴
Code
제출 날짜
@9/26/2021
메모리
2024 KB
시간
0 ms
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
std::string g_heystack;
std::string g_needle;
int g_answer;
void pre_setting(void)
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
}
void input_action(void)
{
std::getline(std::cin, g_heystack);
std::getline(std::cin, g_needle);
}
void solution(void)
{
auto begin = std::begin(g_heystack);
auto end = std::end(g_heystack);
while (true)
{
begin = std::search(begin, end, std::begin(g_needle), std::end(g_needle));
if (begin != end)
{
++g_answer;
std::advance(begin, g_needle.size());
}
else
break;
}
}
void output_action(void)
{
std::cout << g_answer;
}
int main(void)
{
pre_setting();
input_action();
solution();
output_action();
return (0);
}
C++
복사