D. AB-string (thinking)

Portal

Meaning of the questions: to give you a string of length n, string composed only of AB.

   I ask you how many good string there is this string; (continuous period of substring)

   good string definition is: a string all the elements are part of a palindrome string this string.

   For example: AAABBB is a good string, because both belong to AAA AAA this palindrome. BBB BBB also belong to this palindrome.

      ABAA also a good string, because the first A palindrome belong to the ABA, the last part of A AA this palindrome.

 

Solution: thinking problems. BBA found that only ABB or this does not work, the other is how to pick the line. 

   If all legal, then for each location i, to the right point as the substring, the point can be chosen with a left i - 1 th.

   That classification for i to the right end point, can not choose the left point.

   1, if like ABBBA, such, that you A right end to the second point can not be selected is between two consecutive B-A length of that period.

   2, B for ABBABBBB you to the far right is the right point, is not selected to the second A is the left end point of this period, the other can be selected.

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define dep(i,j,k) for(int i=k;i>=j;i--)
#define INF 0x3f3f3f3f
#define mem(i,j) memset(i,j,sizeof(i))
#define make(i,j) make_pair(i,j)
#define pb push_back
#define Pi acos(-1.0)
using namespace std;
const int N = 3e5 + 5;
char a[N];
int main() {
    int n;
    scanf("%d %s",&n, a + 1);
    LL ans = 0; int like = 0;
    rep(i, 1, n) {
        if (i == 1 || a[i] == a[i - 1]) like++, ans += i - 1 - (like < i);
        else ans += i - 1 - like, like = 1;
    }
    printf("%lld\n",ans);
    return 0;
}
View Code

 

   

Guess you like

Origin www.cnblogs.com/Willems/p/11911144.html