結果

問題 No.884 Eat and Add
ユーザー tarattata1
提出日時 2019-09-14 00:17:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 1,138 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 68,308 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-04 10:40:57
合計ジャッジ時間 994 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, char**)’:
main.cpp:30:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |     scanf("%s", str);
      |     ~~~~~^~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

char str[200005];
int dp[200005][2];

int main(int argc, char* argv[])
{
    scanf("%s", str);
    int n=strlen(str);
    if(n==1) {
        if(str[0]=='0') printf("0\n");
        else printf("1\n");
        return 0;
    }
    int i;
    dp[0][0]=1;
    dp[0][1]=0;
    for(i=1; i<n; i++) {
        if(str[i]=='0') {
            dp[i][0]=MIN(dp[i-1][0],   dp[i-1][1]+2);
            dp[i][1]=MIN(dp[i-1][0]+1, dp[i-1][1]+1);
        }
        else {
            dp[i][0]=MIN(dp[i-1][0]+1, dp[i-1][1]+2);
            dp[i][1]=MIN(dp[i-1][0],   dp[i-1][1]);
        }
    }
    printf("%d\n", MIN(dp[n-1][0], dp[n-1][1]+2));


    return 0;
}
0