-
Notifications
You must be signed in to change notification settings - Fork 80
/
special_palindrome_again.cpp
61 lines (43 loc) · 1.07 KB
/
special_palindrome_again.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
#include <bits/stdc++.h>
#include <string>
using namespace std;
long substrCount(int n, string s) {
string cs;
long ep_count;
char curr_char;
const char* s_chars;
vector<int> ocurr(n);
curr_char= '\0';
s_chars = s.c_str();
int str_index = -1;
for(int i = 0; i < n ; i++){
if(i == 0 || s_chars[i] != curr_char){
str_index++;
curr_char = s_chars[i];
cs.append(1, curr_char);
}
ocurr[ str_index ]++;
}
for(int i = 0; i < cs.size(); i++){
ep_count+=(ocurr[i]*(ocurr[i]+1))/2;
}
for(int i = 1; i < cs.size()-1 ; i++){
if( ocurr[i] == 1 && cs[i-1] == cs[i+1] ){
ep_count+= min(ocurr[i-1], ocurr[i+1]);
}
}
return ep_count;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string s;
getline(cin, s);
long result = substrCount(n, s);
fout << result << "\n";
fout.close();
return 0;
}