Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/80723bae951328a690bb to your computer and use it in GitHub Desktop.
Save jianminchen/80723bae951328a690bb to your computer and use it in GitHub Desktop.
Bear and Steady Gene algorithm, Julia's practice - it took her more than 40 minutes to write, 20 minutes to come out the idea.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BearAndSteadyGene
{
/*
* Failed test case:
* input:
* 40
TGATGCCGTCCCCTCAACTTGAGTGCTCCTAATGCGTTGC
* output
* 5
*/
class BearAndSteadyGene
{
static void Main(string[] args)
{
string s = Console.ReadLine();
int n = Convert.ToInt16(s);
string s2 = Console.ReadLine();
int len = s2.Length;
string s3 = s2;
if (len >= 2 && s2[len - 2] == '\r' && s2[len - 1] == '\n')
{
s3 = s2.Substring(0, len - 2);
Console.WriteLine(len - 2 - getMaxLength(s3, len - 2));
}
else
Console.WriteLine(len - getMaxLength(s3, len));
}
private static string ReadLine()
{
int len = 500000 + 10;
Stream inputStream = Console.OpenStandardInput(len);
byte[] bytes = new byte[len];
int outputLength = inputStream.Read(bytes, 0, len);
char[] chars = Encoding.UTF7.GetChars(bytes, 0, outputLength);
return new string(chars);
}
public static int getMaxLength(string s, int n)
{
IList<int[]> list = new List<int[]>();
int maxL = getLeftSideMax(s, n, list);
IList<int[]> listR = new List<int[]>();
int maxR = getRightSideMax(s, n, listR);
int max = int.MinValue;
int k = maxR -1;
for(int i=0;i< maxL; i++)
{
if(i==0){
max = Math.Max(max, maxR);
continue;
}
for(int j = k; j>=0; j--)
{
if(CheckValue(list[i], listR[j], n))
{
max = Math.Max(max, i+j +2); // bug001 - orginal forget to add 2, bug: i+j, should be i+j+2
break;
}
}
}
return max;
}
public static bool CheckValue(int[] A, int[] B, int n)
{
for(int i=0;i<4; i++)
{
if(A[i] + B[i] > n/4)
return false;
}
return true;
}
/*
* The function can be rewritten in less than 10 lines of code
* 64 lines -> 10 lines
*
*/
public static int getLeftSideMax(string s, int n, IList<int[]> list)
{
int m = n / 4;
int[] cA = new int[4];
int i=0;
for(; i< n; i++)
{
char c = s[i];
if(isA(c))
{
if(cA[0] <m)
{
cA[0]++;
addToList(cA, list, i);
}
else
{
addToList(cA, list, i);
break;
}
}
else if(isC(c))
{
if(cA[1] <m)
{
cA[1]++;
addToList(cA, list, i);
}
else
{
addToList(cA, list, i);
break;
}
}
else if(isG(c))
{
if(cA[2] <m)
{
cA[2]++;
addToList(cA, list, i);
}
else
{
addToList(cA, list, i);
break;
}
}
else if(isT(c))
{
if( cA[3] <m)
{
cA[3]++;
addToList(cA, list, i);
}
else
{
addToList(cA, list, i);
break;
}
}
}
return i;
}
private static void addToList(int[] cA, IList<int[]> list, int index)
{
int[] A = new int[4];
for(int i=0;i<4;i++)
A[i] = cA[i];
list.Add(A);
}
private static int getRightSideMax(string s, int n, IList<int[]> list)
{
return getLeftSideMax(reverse(s), n, list);
}
private static string reverse(string s)
{
if(s == null || s.Length == 0)
return s;
StringBuilder sb = new StringBuilder();
for(int i=0;i<s.Length; i++)
sb.Append(s[s.Length-i-1]);
return sb.ToString();
}
private static bool isA(char c)
{
return c-'A'==0;
}
private static bool isC(char c)
{
return c-'C'==0;
}
private static bool isG(char c)
{
return c-'G'==0;
}
private static bool isT(char c)
{
return c-'T'==0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment