CF EDU 103 D - Journey

D - Journey

dp

状态设计 \(f[p][t][x]\) 为从 p 店出发,向 x 方向走,当前是第 t 天,还能走多少个城市(包括 p)

其中 x 为 0 表示向左,1 表示向右

t 为 0 表示边的方向是初始方向,t 为 1 表示边的方向是初始方向相反

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
int n;
int f[N][2][2], op[N];
int d[N][2];
int dp(int p, int t, int x)//当前在p,第 t 天,向 x 方向走
{
	if (p < 0 || p > n)
		return 0;
	int &now = f[p][t][x];
	if (now)
		return now;
	now = 1;
	int dx = (x ? 1 : -1);
	if (d[p][x] ^ t == 1)
		now += dp(p + dx, t ^ 1, x);
	return now;
}

void init()
{
	fill(d[0], d[0] + 2 * n + 2, 0);
	for (int i = 0; i <= n; i++)
		for (int j = 0; j < 2; j++)
			for (int k = 0; k < 2; k++)
				f[i][j][k] = 0;
}
int main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	while(T--)
	{
		string s;
		cin >> n >> s;
		s = " " + s;
		init();
		for (int i = 1; i < s.size(); i++)
		{
			char ch = s[i];
			if (ch == 'L')
				d[i][0] = 1;
			else
				d[i-1][1] = 1;
		}
		for (int i = 0; i <= n; i++)
		{
			int ans = dp(i, 0, 0) + dp(i, 0, 1) - 1;
			cout << ans << " ";
		}
		cout << endl;
	}
	return 0;
}

posted @ 2022-05-25 22:05  hzy0227  阅读(17)  评论(0编辑  收藏  举报