알고리즘/알고리즘 문제풀이

Codeforces Round #805 (Div. 3) - B

b1ackhand 2022. 7. 11. 23:13

문제 출처:

https://codeforces.com/contest/1702/problem/B

 

문제 분석:

하루에 단어를 3개 기억한다. 최소한의 날짜로 전체의 단어를 적으려면 몇일 걸리는가에 관한 문제이다.

lollipops

l o i 기억시 첫날 lolli

p o s 기억 후 이틀째 pops 

 

문제 해결:

arr안에 기억한 알파벳을 저장하고 

used로 3개가 넘어가면 날짜를 체크한다.

used가 3인 순간 체크 해버리면 

lollill 이런 단어는 체크가 안된다.

lolli 여기서 끊길것이다.

 

내 소스코드:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <list>
#include <set>
#include <string.h>
#include <map>
#include <limits.h>
#include <stdlib.h>
#include <typeinfo>
#include <bitset>
 
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define range(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
 
const int INF = 987654321;
 
int testcase;
 
int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> testcase;
 
 
    while (testcase--)
    {
        int used = 0;
        int arr[30] = { 0, };
        string tmp;
        cin >> tmp;
        int ans = 0;
 
        for (int i = 0; i < tmp.size(); i++)
        {
            if (arr[tmp[i] - 'a'] == 1)
                continue;
 
            arr[tmp[i] - 'a'] = 1;
            used++;
 
            if (used > 3)
            {
                ans++;
                memset(arr, 0, sizeof(arr));
                used = 1;
                arr[tmp[i] - 'a'] = 1;
            }
        } 
        if (used != 0)
            ans++;
        cout << ans << "\n";
    }
 
 
    return 0;
}

 

고찰 :

문제 이해를 잘못해서 한번 코드를 다 짠다음 다시 짰다. 여기서 시간이 많이 끌린게 조금 아쉽다.