-
Notifications
You must be signed in to change notification settings - Fork 1
/
BearAndSteadyGene.cpp
110 lines (88 loc) · 1.68 KB
/
BearAndSteadyGene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// problem: "https://www.hackerrank.com/challenges/bear-and-steady-gene/problem"
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
string s;
cin>>n;
cin>>s;
int A,C,G,T,L;
A = C = G = T = 0;
int i;
L = -1;
for(i=0;i!=s.length();i++)
{
if(s[i]=='A')
A++;
else if(s[i]=='C')
C++;
else if(s[i]=='G')
G++;
else
T++;
if(L==-1 && (A>n/4 || G>n/4 || C>n/4 || T>n/4))
L = i;
}
int r,cA,cT,cG,cC;
cA = cG = cT = cC = 0;
r = 0;
while(A-cA>n/4 || C-cC>n/4 || T-cT>n/4 || G-cG>n/4)
{
if (s[r] == 'A')
cA++;
else if (s[r] == 'C')
cC++;
else if (s[r] == 'G')
cG++;
else
cT++;
r++;
}
if (s[r] == 'A')
cA++;
else if (s[r] == 'C')
cC++;
else if (s[r] == 'G')
cG++;
else
cT++;
// cout<<L<<' '<<r;
// cout<<endl;
int sz = n;
i = 0;
while(i<=L && r!=n)
{
if(A-cA<=n/4 && C-cC<=n/4 && G-cG<=n/4 && T-cT<=n/4)
{
if(s[i]=='A')
cA--;
else if(s[i]=='C')
cC--;
else if(s[i]=='G')
cG--;
else
cT--;
sz = min(sz,r-i+1);
i++;
}
else
{
r++;
if(s[r]=='A')
cA++;
else if(s[r]=='C')
cC++;
else if(s[r]=='G')
cG++;
else
cT++;
}
}
if(sz!=n)
cout<<sz;
else
cout<<0;
return 0;
}