-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspecial_string_again_20191101_1032_1.cpp
More file actions
58 lines (42 loc) · 1.13 KB
/
special_string_again_20191101_1032_1.cpp
File metadata and controls
58 lines (42 loc) · 1.13 KB
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
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
// Complete the substrCount function below.
long substrCount(int n, const string& s)
{
long res=0;
long int d[3] = {0,0,0};
for(unsigned int i=1; i<s.size(); ++i)
{
if(s[i] != s[d[2]])
{
// Adding T1
const auto l1=(i-d[2]);
const auto add_l1 = l1*(l1+1)/2;
res += add_l1;
// Adding T2
if( (d[2] > d[1]) && (d[1] > d[0]) && (d[2]-d[1]==1) && (s[d[2]] != s[d[1]]) && (s[d[2]] == s[d[0]]) )
{
const auto l2 = min(i - d[2], d[1] - d[0]);
const auto add_l2 = l2;
res += add_l2;
}
d[0] = d[1]; d[1] = d[2]; d[2] = i;
}
}
return res;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string s;
getline(cin, s);
// NOTE: Changed also this part for convenience
long result = substrCount(n, s+'0');
fout << result << "\n";
fout.close();
return 0;
}