본문 바로가기

코딩 테스트/프로그래머스 코딩테스트

[프로그래머스] 크기가 작은 부분 문자열

https://school.programmers.co.kr/learn/courses/30/lessons/147355

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

#include <string>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int solution(string t, string p) {
    int answer = 0;
    
    // 문자열 p의 크기만큼 쪼개기
    int N = t.size();
    int M = p.size();
    
    // 7, 3 -> 5
    int iter = N-M+1;
    
    vector<string> vec_str;
    for(int i=0; i<iter; i++){
        string str;
        str.resize(M);
        
        int idx = 0;
        for(int j=i; j<i+M; j++){
            str[idx] = t[j];
            idx++;
        }
        // cout << "str = " << str << endl;
        vec_str.push_back(str);
    }
    
    // 숫자로 변환
    vector<long> vec_num;
    for(int i=0; i<vec_str.size(); i++){
        long num = stol(vec_str[i]);
        vec_num.push_back(num);
        
        cout << vec_num[i] << endl;
    }
    
    long p_num = stol(p);
    for(int i=0; i<vec_num.size(); i++){
        if(p_num >= vec_num[i]){
            answer++;
        }
    }
    
    
    return answer;
}

 

long 자료형 오랜만에 써본다.ㅎ