• C


    题目链接:http://codeforces.com/problemset/problem/17/C

    ick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations:

    • to take two adjacent characters and replace the second character with the first one,
    • to take two adjacent characters and replace the first character with the second one

    To understand these actions better, let's take a look at a string «abc». All of the following strings can be obtained by performing one of the described operations on «abc»: «bbc», «abb», «acc». Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string «abc»: |a| = 1, |b| = 1, |c| = 1, and for string «bbc»: |a| = 0, |b| = 2, |c| = 1.

    While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is  - 1 ≤ |a| - |b| ≤ 1,  - 1 ≤ |a| - |c| ≤ 1 и  - 1 ≤ |b| - |c| ≤ 1.

    Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string s. This number should be calculated modulo 51123987.

    Input

    The first line contains integer n (1 ≤ n ≤ 150) — the length of the given string s. Next line contains the given string s. The initial string can be balanced as well, in this case it should be counted too. The given string s consists only of characters a, b and c.

    Output

    Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string s, modulo 51123987.

    题目大意:给定一个长度为n的只含有a,b,c的字符串,对该字符串进行多次(或0次)以下操作:将某个位置的字符变为其左边的或右边的。求操作后所得字符串满足3种字符两两数量差不超过1的有多少个?

    思路:

    很容易想到是dp的题,通过观察可以知道,

    变换前后字符串中字符的相对位置不会发生改变

    也就是说,将前后字符串unique后,B是A的子序列(相邻有相同的则留下一个)

    dp[i][j][k][l]代表 在第i位 有j个a k个b l个c的取值个数

    所以可以用当前状态更新后面的状态,这里有一个nxt数组,代表下一个a/b/c出现的位置,更新这个位置就行了(因为要保证相对位置不变)

    看代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<vector>
    #include<stack>
    #include<map>
    #include<queue>
    #include<cmath>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ull;
    #define sc1(a) scanf("%lld",&a)
    #define pf1(a) printf("%lld
    ",a)
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    const LL INF=1e18;
    const ull base=2333;
    const int maxn=1e2+55;
    const int maxm=1e4+50;
    const int maxv=1e6+5;
    const int mod=51123987;
    char a[maxn];
    int dp[maxn][55][55][55];
    int nxt[maxn][5];
    bool check(LL x,LL y,LL z)
    {
        if(abs(x-y)>1) return false;
        if(abs(x-z)>1) return false;
        if(abs(y-z)>1) return false;
        return true;
    }
    int main()
    {
        LL N;sc1(N);
        for(LL i=1;i<=N;i++) cin>>a[i];
        int lena=1;
        for(int i=2;i<=N;i++)
        {
            if(a[i]!=a[i-1]) a[++lena]=a[i];
        }
        for(LL i=lena;i>=1;i--)
        {
            nxt[i][0]=nxt[i+1][0];
            nxt[i][1]=nxt[i+1][1];
            nxt[i][2]=nxt[i+1][2];
            nxt[i][a[i]-'a']=i;
        }
        dp[1][0][0][0]=1;
        LL len=(N+2)/3;
        LL ans=0;
        for(LL i=1;i<=lena;i++)
        {
            for(LL j=0;j<=len;j++)
            {
                for(LL k=0;k<=len;k++)
                {
                    for(LL l=0;l<=len;l++)
                    {
                        if(dp[i][j][k][l])
                        {
                            if(j+k+l==N&&check(j,k,l)) ans=(ans+dp[i][j][k][l])%mod;
                            if(nxt[i][0]!=0) dp[nxt[i][0]][j+1][k][l]=(dp[nxt[i][0]][j+1][k][l]+dp[i][j][k][l])%mod;//当前位置取值为a
                            if(nxt[i][1]!=0) dp[nxt[i][1]][j][k+1][l]=(dp[nxt[i][1]][j][k+1][l]+dp[i][j][k][l])%mod;//b
                            if(nxt[i][2]!=0) dp[nxt[i][2]][j][k][l+1]=(dp[nxt[i][2]][j][k][l+1]+dp[i][j][k][l])%mod;//当前位置取值为c
                        }
                    }
                }
            }
        }
        pf1(ans);
        return 0;
    }
    /**
    
    */
  • 相关阅读:
    l1-010
    l1-009
    L1-008修改
    l1-008
    Codeforces Round #406 (Div. 2)
    求N!的长度【数学】 51nod 1058 1130
    51nod 1090 & 1267 【二分简单题】
    Codeforces Round #405 (Div. 2)
    Codeforces Round #404 (Div. 2)
    PAT 天梯赛真题集(L2、L3)
  • 原文地址:https://www.cnblogs.com/caijiaming/p/12499985.html
Copyright © 2020-2023  润新知