zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 74 (Rated for Div. 2)D. AB-string

    D. AB-string

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    The string t1t2…tkt1t2…tk is good if each letter of this string belongs to at least one palindrome of length greater than 1.

    A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not.

    Here are some examples of good strings:

    • tt = AABBB (letters t1t1, t2t2 belong to palindrome t1…t2t1…t2 and letters t3t3, t4t4, t5t5 belong to palindrome t3…t5t3…t5);
    • tt = ABAA (letters t1t1, t2t2, t3t3 belong to palindrome t1…t3t1…t3 and letter t4t4 belongs to palindrome t3…t4t3…t4);
    • tt = AAAAA (all letters belong to palindrome t1…t5t1…t5);

    You are given a string ss of length nn, consisting of only letters A and B.

    You have to calculate the number of good substrings of string ss.

    Input

    The first line contains one integer nn (1≤n≤3⋅1051≤n≤3⋅105) — the length of the string ss.

    The second line contains the string ss, consisting of letters A and B.

    Output

    Print one integer — the number of good substrings of string ss.

    Examples

    input

    Copy

    5
    AABBB
    

    output

    Copy

    6
    

    input

    Copy

    3
    AAA
    

    output

    Copy

    3
    

    input

    Copy

    7
    AAABABB
    

    output

    Copy

    15
    

    Note

    In the first test case there are six good substrings: s1…s2s1…s2, s1…s4s1…s4, s1…s5s1…s5, s3…s4s3…s4, s3…s5s3…s5 and s4…s5s4…s5.

    In the second test case there are three good substrings: s1…s2s1…s2, s1…s3s1…s3 and s2…s3s2…s3.

    随便在纸上写一写我们就可以发现其实不满足要求的只有一种字符串

    那就使abbbbbbbbbbb或aaaaaaaaaaaab

    在某一测出现了单独字符 

    所以问题就转化成了统计以上字符串的个数

    我们开一个vector记录相邻的相同项

    用全部的数减去不合法数目即可

    #include<bits/stdc++.h>
    using namespace std;
    char s[300005];
    vector<int>ve;
    int main()
    {
        int n;
        scanf("%d",&n);
        scanf("%s",s+1);
        int pos=1;
        for(int i=2;i<=n;i++)
        {
            if(s[i]==s[i-1])
            {
                pos++;
            }
            else
            {
                ve.push_back(pos);
                pos=1;
            }
            if(i==n) ve.push_back(pos);
        }
        long long ans=1LL*n*(n-1)/2;
        if(ve.size()>=2)
        {
            for(int i=0;i<ve.size();i++)
            {
                if(i==0||i==ve.size()-1) ans-=ve[i];
                else ans-=ve[i]*2;
            }
            ans+=ve.size()-1;
        }
        printf("%I64d
    ",ans);
    }
    
  • 相关阅读:
    几何+点与线段的位置关系+二分(POJ2318)
    二维凸包模板(凸包重心,周长,面积,直径,最大三角形,最小环绕矩形)
    边的双联通+缩点+LCA(HDU3686)
    三维凸包(两个没有公共点)经过旋转平移后使其重心相距最近(POJ3862)
    三维凸包求凸包表面的个数(HDU3662)
    三维凸包求其表面积(POJ3528)
    三维凸包求重心到面的最短距离(HDU4273)
    三维凸包求内部一点到表面的最近距离(HDU4266)
    三维凸包模板
    判断点与多边形的位置关系
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852193.html
Copyright © 2011-2022 走看看