Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. class Solution
  5. {
  6.  
  7.     static bool match(Dictionary<char, int> a, Dictionary<char, int> b, char c)
  8.     {
  9.  
  10.         if (!a.ContainsKey(c))
  11.             return false;
  12.         else
  13.         {
  14.             if (a[c] - 1 < b[c])
  15.                 return true;
  16.             else return false;
  17.         }
  18.     }
  19.  
  20.     static void Main(String[] args)
  21.     {
  22.         int n = int.Parse(Console.ReadLine());
  23.         string gene = Console.ReadLine();
  24.  
  25.        
  26.         Dictionary<char, int> dic = new Dictionary<char, int>();
  27.         dic.Add('A', 0);
  28.         dic.Add('T', 0);
  29.         dic.Add('G', 0);
  30.         dic.Add('C', 0);
  31.  
  32.         //counting A, C, T, G
  33.         for (int i = 0; i < n; i++)
  34.         {
  35.             dic[gene[i]]++;
  36.         }
  37.  
  38.         //determining how much do we need to change
  39.        
  40.         int[] arr = new int[4];
  41.         arr[0] = dic['A'] - n / 4;
  42.         arr[1] = dic['T'] - n / 4;
  43.         arr[2] = dic['G'] - n / 4;
  44.         arr[3] = dic['C'] - n / 4;
  45.  
  46.         //for fisrt substring search
  47.         Dictionary<char, int> needToFind = new Dictionary<char, int>();
  48.         //for later comparsion
  49.         Dictionary<char, int> needToFind_perma = new Dictionary<char, int>();
  50.         //currenty how much searched letters is the substring
  51.         Dictionary<char, int> currenlyHolds = new Dictionary<char, int>();
  52.  
  53.         //if arr[i] > 0, we need too change at least arr[i] letters
  54.         if (arr[0] > 0)
  55.         {
  56.             needToFind.Add('A', arr[0]);
  57.             currenlyHolds.Add('A', 0);
  58.         }
  59.         if (arr[1] > 0)
  60.         {
  61.             currenlyHolds.Add('T', 0);
  62.             needToFind.Add('T', arr[1]);
  63.         }
  64.         if (arr[2] > 0)
  65.         {
  66.             currenlyHolds.Add('G', 0);
  67.             needToFind.Add('G', arr[2]);
  68.         }
  69.         if (arr[3] > 0)
  70.         {
  71.             currenlyHolds.Add('C', 0);
  72.             needToFind.Add('C', arr[3]);
  73.         }
  74.  
  75.         //saving for later comparsion
  76.         foreach (KeyValuePair<char, int> a in needToFind)
  77.         {
  78.             needToFind_perma.Add(a.Key, a.Value);
  79.         }
  80.  
  81.         int min = 0;
  82.  
  83.         //determining the minimum number of letters we need to change
  84.         for (int i = 0; i < 4; i++)
  85.         {
  86.             if (arr[i] > 0)
  87.                 min += arr[i];
  88.         }
  89.  
  90.         int howmany = 0;
  91.         int min_ = n;
  92.         int leftIndex = 0;
  93.         bool breaked = false;
  94.  
  95.         //while the first letter found that is in the needToFind dictionary
  96.         while (leftIndex < n && !needToFind.ContainsKey(gene[leftIndex]))
  97.         {
  98.             leftIndex++;
  99.         }
  100.  
  101.         int rightIndex = leftIndex;
  102.  
  103.         //while all the letters we want to change are found
  104.         //(when the needToFind is empty)
  105.         while (rightIndex < n && needToFind.Count != 0)
  106.         {
  107.             //saving every changeable letter beetween the two index
  108.             if (currenlyHolds.ContainsKey(gene[rightIndex]))
  109.                 currenlyHolds[gene[rightIndex]]++;
  110.  
  111.             howmany++;
  112.  
  113.             if (needToFind.ContainsKey(gene[rightIndex]))
  114.             {
  115.                 needToFind[gene[rightIndex]]--;
  116.                 if (needToFind[gene[rightIndex]] == 0)
  117.                     needToFind.Remove(gene[rightIndex]);
  118.             }
  119.  
  120.             rightIndex++;
  121.  
  122.         }
  123.  
  124.         //leaving some of the first letters in the substring
  125.         //while we still have enough in it to satisfy the conditions
  126.         while(leftIndex < n && !match(currenlyHolds, needToFind_perma, gene[leftIndex]))
  127.         {
  128.             if (needToFind_perma.ContainsKey(gene[leftIndex]) && needToFind_perma[gene[leftIndex]] != 0)
  129.                 currenlyHolds[gene[leftIndex]]--;
  130.             leftIndex++;
  131.             howmany--;
  132.         }
  133.  
  134.         char saved = ' ';
  135.         if(leftIndex < n)
  136.         //the first letter after the search that we must not leave
  137.         saved = gene[leftIndex];
  138.  
  139.         if (howmany == min)
  140.         {
  141.             //if the min reached, break, stop the search
  142.             Console.WriteLine(min);
  143.             breaked = true;
  144.         }
  145.         else if( leftIndex < n)
  146.         {
  147.             if (howmany < min_)
  148.             {
  149.                 min_ = howmany;
  150.             }
  151.  
  152.             while (leftIndex < n - min - 1)
  153.             {
  154.                 //moving right
  155.                 leftIndex++;
  156.                 howmany--;
  157.  
  158.                 //same as above
  159.                 while (rightIndex < n &&!match(currenlyHolds, needToFind_perma, gene[leftIndex]))
  160.                 {
  161.                     if (needToFind_perma.ContainsKey(gene[leftIndex]) && needToFind_perma[gene[leftIndex]] != 0)
  162.                         currenlyHolds[gene[leftIndex]]--;
  163.                     leftIndex++;
  164.                     howmany--;
  165.                 }
  166.  
  167.                 howmany++;
  168.  
  169.                 //finding the first occurence of the saved letter after the substring
  170.                 while (rightIndex < n && gene[rightIndex] != saved)
  171.                 {
  172.                     if (currenlyHolds.ContainsKey(gene[rightIndex]))
  173.                         currenlyHolds[gene[rightIndex]]++;
  174.                     rightIndex++;
  175.                     howmany++;
  176.                 }
  177.  
  178.                 rightIndex++;
  179.                 //saving a new letter
  180.                 saved = gene[leftIndex];
  181.  
  182.                 if (howmany == min)
  183.                 {
  184.                     Console.WriteLine(min);
  185.                     breaked = true;
  186.                     break;
  187.                 }
  188.                 if (howmany < min_)
  189.                     min_ = howmany;
  190.             }
  191.         }
  192.  
  193.         //if not reached the theoretical minimum
  194.         if (breaked == false)
  195.             Console.WriteLine(min_);
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement