Queue CodeForces - 353D (思维dp)

https://codeforces.com/problemset/problem/353/D

大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧

$dp[i]$为位置$i$处的$F$复位所花费时间, 有

$dp[i] = max(dp[i-1]+1,cnt_i)$, $cnt_i$为前$i$位$M$的个数

$dp$最大值即为答案

#include <iostream>
#include <algorithm>
#include <cstdio>
#define REP(i,a,n) for(int i=a;i<=n;++i)

using namespace std;
const int N = 1e6+10, INF = 0x3f3f3f3f;
char s[N];

int main() {
    scanf("%s", s);    
    int pos = 0, n = strlen(s);
    for (; s[pos]=='F'; ++pos);
    int ans = 0, cnt = 0;
    REP(i,pos,n-1) {
        if (s[i]=='M') ++cnt;
        else ans = max(ans+1, cnt);
    }
    printf("%d\n", ans);
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10329593.html
今日推荐