其他分享
首页 > 其他分享> > PAT 计数

PAT 计数

作者:互联网

https://www.acwing.com/problem/content/1585/

状态机的解法

#include <iostream>
#include <cstring>

using namespace std;

const int N = 100010, MOD = 1e9 + 7;

int n;
char s[N], p[] = " PAT";
int f[N][4];

int main()
{
    cin >> s + 1;
    n = strlen(s + 1);

    f[0][0] = 1;
    for (int i = 1; i <= n; i ++ )
        for (int j = 0; j <= 3; j ++ ) 
        {
            f[i][j] = f[i - 1][j];
            if (s[i] == p[j]) f[i][j] = (f[i][j] + f[i - 1][j - 1]) % MOD;
        }

    cout << f[n][3] << endl;

    return 0;
}

前缀和+状态机的思想 的解法

#include <iostream>
using namespace std;
const int mod = 1e9 + 7;
long long p, a, b, res;
int main()
{
    string str;
    cin >> str;
    for (auto c : str)
        if (c == 'P') p ++;
        else if (c == 'A') b += p;
        else res = (res + b) % mod;
    cout << res;
}

标签:PAT,int,res,状态机,计数,str,include
来源: https://www.cnblogs.com/xjtfate/p/16614238.html