백준 2195 문자열 복사 / C++

이유참치·2025년 12월 15일

백준

목록 보기
65/249

문제 : 2195

풀이 point

S의 부분문자열 중 P와 일치하는 가장 긴 부분 문자열을 찾아주면 된다.

코드

//백준 2195, 문자열 복사

#include <iostream>
#include <vector>

int main (){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    std::string s, p;

    std::cin >> s >> p;

    int cnt{0};

    for(int i{0}; i<p.length();){
        int max{0};
        for(int j{0}; j<s.length(); ++j){
            int tmp{0};
            while(p[i+tmp] == s[j+tmp]){
                ++tmp;
            }
            max = std::max(max, tmp);
        }
        i += max;
        ++cnt;
    }
    
    std::cout << cnt;

    return 0;
}
profile
임아리 - 대학생

0개의 댓글