Untitled
unknown
plain_text
a year ago
1.1 kB
11
Indexable
#include <bits/stdc++.h>
using namespace std;
// Complete the substrCount function below.
long substrCount(int n, string s) {
int l = 0, r = 1;
long res = 0;
while (l < n && r < n) {
if (l >= r) {
r = l + 1;
continue;
}
if (s[l] == s[r]) {
r++;
continue;
}
else{
int num = r - l;
res += ((num + 1) * num) / 2;
if(s.substr(l, r - l) == s.substr(r + 1, r - l)){
res += r - l;
l++;
r++;
continue;
}
l++;
r++;
}
}
if (r > l) {
int num = r - l;
res += ((num + 1) * num) / 2;
}
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);
long result = substrCount(n, s);
fout << result << "\n";
fout.close();
return 0;
}
Editor is loading...
Leave a Comment