BOJ-1543번

카테고리

  • 문자열

  • 그리디 알고리즘

  • 브루트포스 알고리즘

아이디어

코드

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

int Check_Keyword_Num(string target, string check);

int main() {
    string a, b;
    getline(cin, a);
    getline(cin, b);
    cout << Check_Keyword_Num(a, b);
    return 0;
}

int Check_Keyword_Num(string target, string check) {
    if (target.length() < check.length())
        return 0;
    int num = 0;
    for (int i = 0; i <= target.length() - check.length(); i++) {
        for (int j = 0; j < check.length(); j++) {
            if (target[i + j] != check[j])
                break;
            if (j == check.length() - 1) {
                num++;
                i = i + j;
            }
        }
    }
    return num;
}

주의할 반례

  1. 검색 문자열이 더 큰 경우가 존재할 수 있다.

좋은 웹페이지 즐겨찾기