카테고리 없음

프로그래머스 옹알이(1) c++

시코. 2024. 2. 22. 18:09
728x90

이전에 머지 솔트랑 퀵소트를하면서 분할 정복이라는 방법을 배웠는데 비슷하게 응용해보았던것 같다

 

큰 흐름은 스트링에서 찾은후 그 찾은 부분을 제외한 앞쪽과 뒷쪽으로 나누어 함수를 더 호출해 길이가 0 이면 트루를

길이가 이 아니고 아무것도 찾을 수 없다면  false를 반환 했다

#include <iostream>
#include <string>
#include <vector>

using namespace std;


vector<string> g_canbable;
int sum = 0;

//중간에 지우면 뒤랑 이어져서 그게 그게 말할수있는건줄 알고 착각하고 지워버림
//디버그 입력시와 리턴에서만 돌려보기
//함수로 나누는게 좋당
bool compstr(string& str)
{
    for (auto compstr = g_canbable.begin(); compstr != g_canbable.end(); compstr++)
        while (1)
        {
            if (str.length() == 0)
                return true;
            int foundindex = str.find(*compstr);
            if (foundindex == string::npos)
            {
                break;
            }
            else
            {
                str.erase(foundindex, compstr->length());
            }
        }
    if (str.length() == 0)
        return true;
    else
        return false;
}

int solution(vector<string> babbling) {
    g_canbable.emplace_back("aya");
    g_canbable.emplace_back("ye");
    g_canbable.emplace_back("woo");
    g_canbable.emplace_back("ma");

    for (auto str = babbling.begin(); str != babbling.end(); str++)
    {
        if (compstr(*str))
        {
            sum++;
        }
            
    }
    return sum;
}

int main()
{
    vector<string> test;
    test.emplace_back("aya");
    test.emplace_back("yee");
    test.emplace_back("u");
    test.emplace_back("maa");
    test.emplace_back("wyeoo");
    int ans = solution(test);
    return 0;
}

 

728x90