• 1238D


    题目:传送门
    思路:good字符串的情况有很多,而坏字符串的情况比较少,所以我们可以考虑容斥,总的字符串有(n+1)*n/2个,我们直接算坏的;
    坏的字符串: ABBBBBB…BB(一个A ,k个B ) 或者 BBB…BBA 或者 A,1<=k<n;

    AC代码

    #include<cstdio>
    #include<cstring>
    #include<cctype>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int N=1e6+5;
    const int inf=0x3f3f3f3f;
    const int mod=1e9+7;
    #define ls (i<<1)
    #define rs (i<<1|1)
    #define fi first
    #define se second
    #define mk make_pair
    #define mem(a,b) memset(a,b,sizeof(a))
    LL read()
    {
        LL x=0,t=1;
        char ch;
        while(!isdigit(ch=getchar())) if(ch=='-') t=-1;
        while(isdigit(ch)){ x=10*x+ch-'0'; ch=getchar(); }
        return x*t;
    }
    char str[N];
    int cnt[N];
    int main()
    {
        LL n=read();
        scanf("%s",str+1);
        str[0]='C';
        int tot=0;
        for(int i=1;i<=n;i++) ///举例:直接把 AAABBBBAA 改为cnt 3 4 2
        {
            if(str[i]==str[i-1]) cnt[tot]++;
            else cnt[++tot]++;
        }
        LL ans=(n+1)*n/2-n;
        for(int i=1;i<tot;i++) ans-=cnt[i]; ///计算不合法的字符串
        for(int i=tot;i>1;i--) ans-=cnt[i];
        printf("%lld
    ",ans+tot-1);///最后加tot-1 是因为两次减法 会重复减去“AB”或者“BA”这种情况;
        return 0;
    }
     
     
    
  • 相关阅读:
    jquery常用语句
    记录一个奇异的问题
    冰块渲染2
    冰块渲染
    GCAlloc 问题一则
    矩阵基础3
    优化 Overdraw 和 GrabPass
    优化平面法线贴图
    环境模拟
    使用 GPU 加速计算
  • 原文地址:https://www.cnblogs.com/DeepJay/p/12025191.html
Copyright © 2020-2023  润新知