HackerRank - beautiful-binary-string 【字符串】
2024-10-19 21:43:44
题意
给出一个 N 位的 01 串
然后 每次 改动 可以将其中的 (0 -> 1) 或者 (1 -> 0)
然后 求 最少几次 改动 使得 这个 01 串 当中 不存在 连续的 010 这样的 子串
思路
其实 我们可以发现 连续的 010 子串 有两种情况
第一种: 0101010 这样包含连续的
第二种: 010010 这样 分开的
对于第一种 我们改 两个 1 之间的 0
对于第二种 我们改 两个0 之间 的 1
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1e9 + 5;
const int MOD = 1e9 + 7;
int main()
{
int n;
string s;
cin >> n >> s;
int m = n - 1;
int ans = 0;
for (int i = 1; i < m; i++)
{
if (s[i] == '1' && s[i - 1] == '0' && s[i + 1] == '0')
{
ans++;
if ((i + 3) <= m && s[i + 2] == '1' && s[i + 3] == '0')
s[i + 1] = '1';
else
s[i] = '0';
}
}
cout << ans << endl;
}
最新文章
- mac安装虚拟机
- 移动 Web 触摸与运动解决方案 AlloyTouch 开源啦
- 关于static/const的作用
- Ubuntu的力量何在?
- c语言趣味
- tlplayer,wzplayer所有平台通用加密测试视频
- 【HDOJ】3033 I love sneakers!
- 基于Tomcat7、Java、WebSocket的服务器推送聊天室
- ReportViewer中设置ServerReport.ReportServerCredentials属性的方法
- LNMP优化
- SEO策略与细节:细节决定成败
- 转:【WebDriver】封装GET方法来解决页面跳转不稳定的问题
- vue笔记未整理
- 重写 console.log()
- 百度地图API的应用
- C++获取数组的长度
- Django2.0 URL配置
- C++11新特性介绍 01
- 洛谷P4165 [SCOI2007]组队(排序 堆)
- Linux 设备总线驱动模型