結果

問題 No.884 Eat and Add
ユーザー leaf_1415leaf_1415
提出日時 2019-09-13 22:00:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 1,000 ms
コード長 856 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 60,460 KB
実行使用メモリ 6,036 KB
最終ジャッジ日時 2023-09-17 07:15:34
合計ジャッジ時間 1,106 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
5,960 KB
testcase_04 AC 6 ms
6,036 KB
testcase_05 AC 6 ms
5,884 KB
testcase_06 AC 6 ms
5,960 KB
testcase_07 AC 5 ms
5,828 KB
testcase_08 AC 5 ms
5,908 KB
testcase_09 AC 5 ms
5,972 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string s;
int nx[200005];
int dp[200005][2];

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> s;
	int n = s.size();
	reverse(s.begin(), s.end());
	s += "00";
	
	nx[n] = n+1;
	for(int i = n-1; i >= 0; i--){
		nx[i] = nx[i+1];
		if(s[i+1] == '0') nx[i] = i+1;
	}
	
	for(int i = 0; i <= n+1; i++){
		for(int j = 0; j < 2; j++){
			dp[i][j] = 1e9;
		}
	}
	dp[0][s[0]-'0'] = 0;
	
	for(int i = 0; i <= n; i++){
		dp[i+1][s[i+1]-'0'] = min(dp[i+1][s[i+1]-'0'], dp[i][0]);
		dp[i+1][s[i+1]-'0'] = min(dp[i+1][s[i+1]-'0'], dp[i][1] + 1);
		if(i < n) dp[nx[i]][1] = min(dp[nx[i]][1], dp[i][1] + 1);
	}
	
	/*for(int i = 0; i <= n+1; i++){
		for(int j = 0; j < 2; j++){
			cout << dp[i][j] << " ";
		}
		cout << endl;
	}*/
	
	cout << dp[n+1][0] << endl;
	
	return 0;
}
0