Advertisement
sylviapsh

Dancing Bits

Dec 27th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. class DancingBits
  4. {
  5.   static void Main()
  6.   {
  7.     //Telerik Academy
  8. //Gergana loves dancing and she also likes bits (she doesn't know what bits really are, but she knows that she likes them). Few days ago she accidently invented a new term - “dancing bits”.
  9. //If you ask her what “dancing bits” mean she will tell you that it’s a sequence of identical bits (so the bits can dance together – zeros can only dance with other zeros, the same applies for ones).
  10. //You are given N positive integer numbers that are converted to binary numeral system and are concatenated together in one big sequence of bits.
  11. //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.
  12. //You are also given a positive integer K - the number of identical bits (zeroes or ones that can dance together).
  13. //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.
  14. //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).
  15. //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.
  16.  
  17.     ushort k = ushort.Parse(Console.ReadLine()),
  18.            n = ushort.Parse(Console.ReadLine());
  19.     string concatenatedNum = "";
  20.     int sum = 0,
  21.         dancingOnesCounter = 0,
  22.         dancingZeroesCounter = 0,
  23.         dancingBitsCounter = 0;
  24.  
  25.     for (int counter = 1; counter <= n; counter++)
  26.     {
  27.       uint number = uint.Parse(Console.ReadLine());
  28.       concatenatedNum += Convert.ToString(number, 2);
  29.     }
  30.  
  31.     int numberOfElements = 0;
  32.     string checkNextSymbol = "0",
  33.            previousElement = "0",
  34.            nextElement = "0";
  35.    
  36.     foreach (char item in concatenatedNum)
  37.     {
  38.       if (numberOfElements == 0)//Checks if we are at the first element.
  39.       {
  40.         previousElement = "0";//If it is the begining of the string, this is the same as having the previous element to be zero.
  41.       }
  42.  
  43.       if (numberOfElements != (concatenatedNum.Length-1)) //Checks if we are at an element that is NOT the element before the last one.
  44.       {
  45.         numberOfElements += 1;
  46.         nextElement = StringInfo.GetNextTextElement(concatenatedNum, numberOfElements);
  47.       }
  48.       else
  49.       {
  50.         nextElement = "0"; //If it is the end of the string, this is the same as having the next element to be zero.
  51.       }
  52.  
  53.       if (item == '1')
  54.       {
  55.         sum++;
  56.       }
  57.       else
  58.       {
  59.         sum = 0;
  60.         previousElement = Convert.ToString(item);
  61.       }
  62.  
  63.       if (sum == k && nextElement == checkNextSymbol && previousElement == checkNextSymbol)
  64.       {
  65.         dancingOnesCounter++;
  66.       }
  67.       if (sum == k)
  68.       {
  69.         sum = 0;
  70.         previousElement = Convert.ToString(item);
  71.       }
  72.     }
  73.  
  74.     numberOfElements = 0;
  75.     checkNextSymbol = "1";
  76.     previousElement = "1";
  77.     foreach (char item in concatenatedNum)
  78.     {
  79.       if (numberOfElements == 0)//Checks if we are at the first element.
  80.       {
  81.         previousElement = "1";//If it is the begining of the string, this is the same as having the previous element to be one.
  82.       }
  83.  
  84.       if (numberOfElements != (concatenatedNum.Length-1)) //Checks if we are at an element that is NOT the element before the last one.
  85.       {
  86.         numberOfElements += 1;
  87.         nextElement = StringInfo.GetNextTextElement(concatenatedNum, numberOfElements);
  88.       }
  89.       else
  90.       {
  91.         nextElement = "1"; //If it is the end of the string, this is the same as having the next element to be one.
  92.       }
  93.  
  94.       if (item == '0')
  95.       {
  96.         sum++;
  97.       }
  98.       else
  99.       {
  100.         sum = 0;
  101.         previousElement = Convert.ToString(item);
  102.       }
  103.  
  104.       if (sum == k && nextElement == checkNextSymbol && previousElement == checkNextSymbol)
  105.       {
  106.         dancingZeroesCounter++;
  107.       }
  108.       if (sum == k)
  109.       {
  110.         sum = 0;
  111.         previousElement = Convert.ToString(item);
  112.       }
  113.     }
  114.     dancingBitsCounter = dancingOnesCounter + dancingZeroesCounter;
  115.     Console.WriteLine(dancingBitsCounter);
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement