Journey

时间:2021-02-02 10:40:54   收藏:0   阅读:0

https://codeforces.com/contest/1476/problem/D

题意

\(n + 1\) 个点和它们之间连接关系, 两点之间是单向的, 每走过一条边, 所有的边反向, 问从某个点开始出发, 能走过的最长的路径是多少

思路

一开始以为要建图, 实际上算是线性dp

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 3e5 + 7;
char s[N];
int l[N], r[N];

int main() {
    IO;
    int _;
    cin >> _;
    while (_--) {
        memset(l, 0, sizeof l);
        memset(r, 0, sizeof r);
        int n;
        cin >> n;
        cin >> s + 1;
        for (int i = 1; i <= n; ++i) 
            if (s[i] == ‘L‘) {
                l[i] = 1;
                if (i >= 2 && s[i - 1] == ‘R‘) l[i] += l[i - 2] + 1;
            }
        for (int i = n - 1; i >= 0; --i) 
            if (s[i + 1] == ‘R‘) {
                r[i] = 1;
                if (i + 2 <= n && s[i + 2] == ‘L‘) r[i] += r[i + 2] + 1;
            }
        for (int i = 0; i <= n; ++i) cout << l[i] + r[i] + 1 << " ";
        cout << ‘\n‘;
    }
    return 0;
}


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!