結果

問題 No.884 Eat and Add
ユーザー snrnsidy
提出日時 2021-06-04 01:34:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 1,050 bytes
コンパイル時間 1,765 ms
コンパイル使用メモリ 193,796 KB
最終ジャッジ日時 2025-01-21 21:23:09
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> 

using namespace std;

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    //greedy 1이 2개 이상이 연속으로 이어져 있으면 하나를 추가해서라도 먹는 게 나음...
    int res = 0;
    string s;
    int len = 0;
    int n;

    cin >> s;

    n = s.length();

    for (int i = n - 1; i >= 0; i--)
    {
        if (s[i] == '0')
        {
            if (len > 0)
            {
                //cout << i << ' ' << s[i] << ' ' << len << '\n';
                if (len == 1)
                {
                    len = 0;
                    res++;
                }
                else
                {
                    len = 1;
                    s[i] = '1';
                    res++;
                }
            }
        }
        else
        {
            len++;
        }
    }
    if (len > 0)
    {
        if (len == 1)
        {
            res++;
        }
        else
        {
            res += 2;
        }
    }

    cout << res << '\n';
    return 0;
}
0