Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //You are given N positive integer numbers that are converted to binary numeral system and are concatenated together in one big sequence of bits.
- //For example: if we have 4 numbers: 5 (101 in binary numeral system), 6 (110 in binary numeral system), 14 (1110 in binary numeral system) and 143
- //(1000111 in binary numeral system) their concatenation will be 101110111010001111.
- //You are also given a positive integer K - the number of identical bits (zeroes or ones that can dance together).
- //Write a program that finds the number of all “dancing bits” (the sequences of equal bits) with a length of exactly K bits.
- //Your program should search in the concatenation of the given N numbers.
- //For example, if we have 4 numbers (5, 6, 14 and 143, the concatenation of their binary representation is 101110111010001111)
- //and we are searching for the total number of all sequences of equal bits with an exact length of 3 bits, the answer will be 3
- //(the sequences are bolded in the concatenation above).
- //In this example we have two sequences of “dancing bits” - "111" consisting of only ones and one sequence of “dancing bits” - "000"
- //consisting of only zeros. Note that the sequence "1111" is not a sequence of exact 3 identical bits.
- class Program
- {
- static void Main()
- {
- int k = new int();
- int n = new int();
- string data = "";
- k = int.Parse(Console.ReadLine());
- n = int.Parse(Console.ReadLine());
- int[] num = new int[n];
- for (int i = 0; i < n; i++)
- {
- num[i] = int.Parse(Console.ReadLine());
- data = data + Convert.ToString(num[i], 2);
- }
- char pattern = '0';
- int dance = new int();
- for (int i = 0; i < data.Length; i++)
- {
- if (data[i] == '0')
- {
- pattern = '0';
- }
- else
- {
- pattern = '1';
- }
- if (i == 0 || data[i - 1] != pattern)
- {
- if (i + k > data.Length)
- {
- continue;
- }
- for (int j = i; j < i + k; j++)
- {
- if (data[j] == pattern)
- {
- if (j == i + k - 1)
- {
- if (j == data.Length - 1 || data[j + 1] != pattern)
- {
- dance++;
- i = i + k - 1;
- break;
- }
- }
- }
- else
- {
- break;
- }
- }
- }
- }
- Console.WriteLine(dance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement