1일1알

백준 13022번 늑대와 올바른 단어 C++ 본문

알고리즘

백준 13022번 늑대와 올바른 단어 C++

영춘권의달인 2022. 11. 29. 10:27

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

 

13022번: 늑대와 올바른 단어

첫째 줄에 단어가 주어진다. 단어는 w, o, l, f로만 이루어져 있으며, 길이는 50을 넘지 않는다.

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;

const int WORDCOUNT = 4;

char cs[4] = { 'w','o','l','f' };
vector<int> cnts;

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

    string str;
    cin >> str;
    int idx = 0;
    int ans = 1;
    bool newStart = true;
    cnts = vector<int>(WORDCOUNT, 0);
    for (int i = 0; i < str.length(); i++) {
        if (idx == 0 && newStart) {
            newStart = false;
            set<int> s;
            for (auto a : cnts) {
                s.insert(a);
            }
            if (s.size() != 1) ans = 0;
            cnts = vector<int>(WORDCOUNT, 0);
        }
        if (cs[idx] == str[i]) {
            cnts[idx]++;
        }
        else {
            ans = 0;
            break;
        }
        if (i == str.length() - 1) break;
        if (str[i + 1] != cs[idx]) {
            idx = (idx + 1) % WORDCOUNT;
            if (idx == 0) newStart = true;
        }
    }
    set<int> s;
    for (auto a : cnts) {
        s.insert(a);
    }
    if (s.size() != 1) ans = 0;
    cout << ans;
}