Sequential game

Problem Description

Sequential detector is a very important device in Hardware experiments. But is very difficult for many students such as HeroWei. In nearest days, his teacher Miss Fang let him simulate sequence detector. But there many sequence here ,and Miss Fang add the difficulty that when she give HeroWei a sequence and a string ,you must tell her how many string here in this sequence. But now Miss Fang simplified the problem for HeroWei, she just give Herowei a sequence and let HeroWei play a game with it.
When comes a character , it
must compare with the rightmost one of sequence, if it's the same with the
coming characters ,the coming one just join the string. Most the right side.
Otherwise all the characters in the sequence must change its value, 0 to 1 and 1
to 0,then the coming one add to its right side. Here is an example, if there is
a sequence '110' when comes a character 0,the sequence become '1100' at last, if
comes a character 1,the sequence become '0011' at last.
It's very difficult
for HeroWei to play the game. Can you help him?

Input

Each line contains a composed by a string of 0s and 1s.the
input is ended by EOF.

Output

For each given sequence you just play with this string as
descripted above.Output a line contains two integers ,which are the number of 0s
and 1s in the sequence after the game.

Sample Input

0
110
1111
1010

Sample Output

1 0
3 0
0 4
4 0 [Tips]
For example ,1010: 1->00->111->0000

描述:

题目大意,有一个01的字符流。当 第 i 个字符和第 i-1 个字符不同时,就把前 i-1个字符,每一个字符都改变一下,如果是0就变为1,如果是1就变为0. 当第 i 个字符和第 i-1个字符一样的时候,就什么事都不发生。对于110, 则是

1->11->000. 然后问你,有多少个0, 多少个1.  这问题就有点简单了啊,很容易可以发现,其实只有两种可能,要么全 0, 要么全 1。我们只要记录 这个字符流中字符 1 的 个数。和字符 0 的个数,然后记录最后一个字符就可以了。

如果这个字符流中只有一种字符,那么结果显而易见的。如果都有,那么就全部变成最后一次出现的那个字符,不信的话,自己随手写一个字符流,去推一推,我就是这么发现的规律。

 #include<bits/stdc++.h>

 using namespace std;

 const int N = ;

 int main() {
char str[N];
while (cin >> str) {
int len_str = strlen(str);
int sum_1 = , sum_0 = ;
for (int i = ; i < len_str; i++) {
if (str[i] == '') sum_1++;
if (str[i] == '') sum_0++;
if (sum_1 != && sum_0 != ) break;
}
if (sum_1 != && sum_0 != ) {
if (str[len_str - ] == '') {
printf("%d %d\n", , len_str);
} else {
printf("%d %d\n", len_str, );
}
} else {
if (sum_1 == ) printf("%d %d\n", len_str, );
else printf("%d %d\n", , len_str);
}
}
return ;
}

最新文章

  1. python中不同包之间调用方法、
  2. HBase基本shell命令
  3. 关于JQuery(最后一点动画效果*)
  4. js 网站顶部导航栏
  5. js图片轮播图
  6. winform中关于panel中滚动条和键盘事件几点体会
  7. cygwin使用
  8. poj3282
  9. prop()、attr()和data()
  10. vue.js学习之组件(下篇)
  11. virtualbox虚拟机NAT模式下不能连接外网
  12. H5canvas基础
  13. linux缺页异常处理--内核空间
  14. jsp 安全
  15. go结构体方法
  16. nginx的https和http共存反向代理配置
  17. Revit API PickPoint过滤条件
  18. idea本地安装 lombok插件
  19. 学习Spring Boot:(十八)Spring Boot 中session共享
  20. 二:nodejs+express+redis+bootstrap table+jquery UI

热门文章

  1. JAVA笔记28-正则表达式(补充、不重要)
  2. python 习题
  3. 12. ClustrixDB 为容错和可用性分配磁盘空间
  4. linux-ssh加密与https安全-9
  5. SpringBoot搭建基于Apache Shiro的权限管理功能
  6. Mui去掉滚动条:
  7. 在Sublime Text 3中配置Python3的开发环境/Build System
  8. 【python学习之五】自定义函数实现用 Python 发送电子邮件
  9. 大数据笔记(二十五)——Scala函数式编程
  10. Route53 health check与 Cloudwatch alarm 没法绑定