1일1알

백준 12933번 오리 C++ 본문

알고리즘

백준 12933번 오리 C++

영춘권의달인 2023. 5. 12. 11:49

https://www.acmicpc.net/problem/12933

 

12933번: 오리

첫째 줄에 영선이가 녹음한 소리가 주어진다. 소리의 길이는 5보다 크거나 같고, 2500보다 작거나 같은 자연수이고, 'q','u','a','c','k'로만 이루어져 있다.

www.acmicpc.net

 

방문 체크를 하면서 계속 반복하면서 올바른 울음소리를 내는 사이클을 얼마나 반복하는지 세면 된다.

우는것을 시작도 안하는 경우, 울다 마는 경우 예외처리를 해줘야한다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

string str;

char c[5] = { 'q','u','a','c','k' };

vector<bool> visited;

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

    cin >> str;
    if (str.length() % 5 != 0) {
        cout << -1;
    }
    else {
        visited = vector<bool>(str.length(), false);
        int ans = 0;
        int cnt = 0;
        while (true) {
            int targetIdx = 0;
            int tmpCnt = 0;
            for (int i = 0; i < str.length(); i++) {
                if (visited[i]) continue;
                if (str[i] != c[targetIdx]) continue;
                targetIdx = (targetIdx + 1) % 5;
                visited[i] = true;
                tmpCnt++;
            }
            if (tmpCnt == 0 || targetIdx != 0) {
                cout << -1;
                return 0;
            }
            cnt += tmpCnt;
            ans++;
            if (cnt == str.length()) break;
        }
        cout << ans;
    }
}

'알고리즘' 카테고리의 다른 글

백준 17085번 십자가 2개 놓기 C++  (1) 2023.05.14
백준 11536번 줄 세우기 C++  (0) 2023.05.13
백준 15671번 오델로 C++  (0) 2023.05.11
백준 25206번 너의 평점은 C++  (0) 2023.05.09
백준 2331번 반복수열 C++  (0) 2023.05.08