1909: Perfect Chocolate

Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 128 Mb     Submitted: 90     Solved: 42


Description

There is a chocolate, is composed of black and white small pieces. Xiao Ming is very fond of this chocolate, and the absolute difference between the number of black pieces and the number of white pieces is not more than 1, he think this chocolate is perfect.
Now Xiao Ming has one this chocolate in his hand, maybe it is not perfect.He will cut it into small blocks, making these small blocks are all perfect, he wanted to know how many times he will cut at least.

Input

There are multiple test cases.
For each test case.There is only one string composed of ‘0’ and ‘1’.’0’ is for the white piece while ‘1’ for the black piece.The length of the string for each case is not more than 100000.

Output

For each test case, you output one line “Case #%d:%d”

Sample Input

10011100010000
1

Sample Output

Case #1:3
Case #2:0

Hint

Source

2017年湖南多校对抗赛第8场

Author

HNU

题解:这个题目也是一道比较简单的题目

第一种方法:每一次选从前面断点开始的最长的满足要求的那个位置断开,成为新的断点  一直到最后面为止

 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <math.h> using namespace std;
char x[];
int vis[];
int sum[],sumb,sumw;;
int main()
{
int cas=;
while(cin>>x)
{
sumb=sumw=;
int len = strlen(x);
for(int i=; i<len; ++i)
{
if(x[i]=='')sumb++,vis[i]=;
else sumw++,vis[i]=-;
}
int ans=;
cout<<"Case #"<<++cas<<":";
int now=;
while(now<len)
{
int sum=,pos=now;
for(int i=now; i<len; i++)
{
sum+=vis[i];
if(sum==||sum==-||!sum) pos=i;
}
ans++;
now=pos+;
}
cout<<ans-<<endl;
} return ;
}

第二种:我以每一块设置一个权值    1多一个权值为1      0多一个为-1          相同为0

我们可以发现  我们分成的每一块  除了不需要分的整个一块1和0的数目相同

其他的情况  我们分出来的每一块的权值一定相同

因为       1  0  可以合成1        1 -1可以合成0       0和1   0和-1

如果相邻的俩块权值是不相同  就可以像上面一样合成一块

 #include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
const int N = 1e5 + ;
string str;
int GetAns(){
int n = str.length();
int sum = ;
for (int i = ; i < n; i++){
if (str[i] == '') sum++;
else sum--;
}
if (sum < ) sum = -sum;
if (sum > ) sum--;
return sum;
}
int main(){
int ca = ;
while (cin >> str){
ca++;
int ans = GetAns();
cout << "Case #" << ca << ":" << ans << endl;
}
return ;
}

csu 1909: Perfect Chocolate的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. [LeetCode] Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  4. C++: Perfect Forwarding

    Link: Rvalue References and Perfect Forwarding in C++0x (https://www.justsoftwaresolutions.co.uk/cpl ...

  5. How to write perfect C code

    Several days ago, I was involved in an argument about choice of C or C++. What I ignored was "l ...

  6. 在执行xp_cmdshell的过程中出错,调用'LogonUserW'失败,错误代码:'1909'

    在上篇文章Could not obtain information about Windows NT group/user 'xxxx\xxxx', error code 0x5里面,我介绍了SQL ...

  7. POJ1274 The Perfect Stall[二分图最大匹配]

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  8. 【leetcode】Perfect Squares (#279)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  9. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

随机推荐

  1. HSSFWorkbook 模版使用

    Java中导入.导出Excel 一.介绍当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用 ...

  2. Python中的各种排序问题

    小书匠python排序 本章目录,快速浏览所需内容: 基本的排序 1.列表(list) 1.1按列表元素大小排序 1.2按列表元素的属性 2.字典(dictory) 3.元组(tuple)排序 3.1 ...

  3. 【原创】go语言学习(二十二)网络编程

    目录 TCP/IP协议介绍 GO快速实现TCP服务端 GO快速实现TCP客户端 UDP协议介绍 UDP编程实例 TCP/IP协议介绍 1.互联网起源 A. 起源于美国五角大楼,它的前身是美国国防部高级 ...

  4. JavaEE的ajax入门

    JavaEE的ajax入门 代码下载 链接:https://pan.baidu.com/s/1pb_sdSmV9Ncs6UIz3q2ztg 提取码:fgx6 复制这段内容后打开百度网盘手机App,操作 ...

  5. PHP开发高可用高安全App后端☆

    第1章 本章先讲解课程所含技术点,并演示相关的项目,让小伙伴对课程有个初步的认知,然后再带领小伙伴进行功能的分析,表的ER总关系图 第2章本章主要讲解课程的一些准备工作知识.包括工具.环境.模板等. ...

  6. mysql触发器个人实战

    create trigger idtriggerbefore insert on flow_management_copy1for each ROWBEGIN SET new.ID= CONCAT(R ...

  7. Go 语言入门(二)方法和接口

    写在前面 在学习 Go 语言之前,我自己是有一定的 Java 和 C++ 基础的,这篇文章主要是基于A tour of Go编写的,主要是希望记录一下自己的学习历程,加深自己的理解 Go 语言入门(二 ...

  8. /bin/sh^M:bad interpreter: No such file or directory问题

    脚本命令正确无误,但是执行脚本的时候报错“/bin/sh^M:bad interpreter: No such file or directory” 原因:该脚本文件在windows系统中编辑过,引入 ...

  9. 设备树中指定的中断触发方式与request_irq中指定的触发方式不一致时,内核会使用哪种中断触发方式呢?

    答:会使用request_irq中指定的触发方式

  10. golang 循环创建闭包 问题排查

    ][]string{ { { "邀请码是什么", "我没有邀请码", "这个邀请码我可以随便填吗", "邀请码可以填他的手机号吗& ...