加载中...

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;
}
posted @ 2022-08-22 20:54  英雄不问出处c  阅读(14)  评论(0编辑  收藏  举报