Advertisement
sashomaga

Dancing Bits

Dec 26th, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.04 KB | None | 0 0
  1. using System;
  2. //You are given N positive integer numbers that are converted to binary numeral system and are concatenated together in one big sequence of bits.
  3. //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
  4. //(1000111 in binary numeral system) their concatenation will be 101110111010001111.
  5. //You are also given a positive integer K - the number of identical bits (zeroes or ones that can dance together).
  6. //Write a program that finds the number of all “dancing bits” (the sequences of equal bits) with a length of exactly K bits.
  7. //Your program should search in the concatenation of the given N numbers.
  8. //For example, if we have 4 numbers (5, 6, 14 and 143, the concatenation of their binary representation is 101110111010001111)
  9. //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
  10. //(the sequences are bolded in the concatenation above).
  11. //In this example we have two sequences of “dancing bits” - "111" consisting of only ones and one sequence of “dancing bits” - "000"
  12. //consisting of only zeros. Note that the sequence "1111" is not a sequence of exact 3 identical bits.
  13. class Program
  14. {
  15.  
  16.  
  17.     static void Main()
  18.     {
  19.         int k = new int();
  20.         int n = new int();
  21.  
  22.         string data = "";
  23.  
  24.         k = int.Parse(Console.ReadLine());
  25.         n = int.Parse(Console.ReadLine());
  26.         int[] num = new int[n];
  27.  
  28.         for (int i = 0; i < n; i++)
  29.         {
  30.             num[i] = int.Parse(Console.ReadLine());
  31.             data = data + Convert.ToString(num[i], 2);
  32.         }
  33.        
  34.             char pattern = '0';
  35.             int dance = new int();
  36.             for (int i = 0; i < data.Length; i++)
  37.             {
  38.                 if (data[i] == '0')
  39.                 {
  40.                     pattern = '0';
  41.                 }
  42.                 else
  43.                 {
  44.                     pattern = '1';
  45.                 }
  46.                 if (i == 0 || data[i - 1] != pattern)
  47.                 {
  48.                     if (i + k > data.Length)
  49.                     {
  50.                         continue;
  51.                     }
  52.                     for (int j = i; j < i + k; j++)
  53.                     {
  54.                         if (data[j] == pattern)
  55.                         {
  56.                             if (j == i + k - 1)
  57.                             {
  58.  
  59.                                 if (j == data.Length - 1 || data[j + 1] != pattern)
  60.                                 {
  61.                                     dance++;
  62.                                     i = i + k - 1;
  63.                                     break;
  64.                                 }
  65.                             }
  66.                         }
  67.                         else
  68.                         {
  69.                             break;
  70.                         }
  71.                     }
  72.                 }
  73.  
  74.             }
  75.             Console.WriteLine(dance);
  76.         }
  77.        
  78.    
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement