Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int fn(int n,string s){int cnt=0;
- for(int i=0;i<n;i++){
- if(s[i]=='a')cnt++;
- else
- cnt=0;
- if(cnt>2)
- return -1;
- }
- int tct = 0;
- for(int i=0;i<n;i++)
- {
- if(s[i] == 'a')
- {
- int ct = 0;
- ct += !!((i-1 >= 0) && (s[i-1] != 'a'));
- ct += !!((i+1 < n) && (s[i+1] != 'a'));
- tct += !!(ct == 2);
- }
- else
- {
- if((i-1 >= 0) && (s[i-1] == 'a'))
- continue;
- else tct += 2;
- }
- }
- if(s[n-1] != 'a')
- tct += 2;
- return tct;
- }
- int main() {
- int n;
- cin>>n;
- string s;
- cin>>s;
- cout<<fn(n,s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement