Sequential game
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?
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 的个数,然后记录最后一个字符就可以了。
如果这个字符流中只有一种字符,那么结果显而易见的。如果都有,那么就全部变成最后一次出现的那个字符,不信的话,自己随手写一个字符流,去推一推,我就是这么发现的规律。
1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int N = 10000; 6 7 int main() { 8 char str[N]; 9 while (cin >> str) { 10 int len_str = strlen(str); 11 int sum_1 = 0, sum_0 = 0; 12 for (int i = 0; i < len_str; i++) { 13 if (str[i] == '1') sum_1++; 14 if (str[i] == '0') sum_0++; 15 if (sum_1 != 0 && sum_0 != 0) break; 16 } 17 if (sum_1 != 0 && sum_0 != 0) { 18 if (str[len_str - 1] == '1') { 19 printf("%d %d\n", 0, len_str); 20 } else { 21 printf("%d %d\n", len_str, 0); 22 } 23 } else { 24 if (sum_1 == 0) printf("%d %d\n", len_str, 0); 25 else printf("%d %d\n", 0, len_str); 26 } 27 } 28 return 0; 29 }