#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 404;
int n, ans = 0;
string s;
int main(){
cin >> n >> s;
s = '/'+ s + '/';
for (int i = 2; i <= n; ++i){
if (s[i-1] == s[i-2] && s[i]!=s[i-1]){
int j = i;
while (j>=2 && s[j-1]==s[j-2]){
swap(s[j], s[j-1]);
j--;
ans++;
}
}
}
reverse(s.begin(), s.end());
for (int i = 2; i <= n; ++i){
if (s[i-1] == s[i-2] && s[i]!=s[i-1]){
int j = i;
while (j>=2 && s[j-1]==s[j-2]){
swap(s[j], s[j-1]);
j--;
ans++;
}
}
}
for (int i = 2; i <= n; ++i)
if (s[i]==s[i-1]) ans = -1;
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |