(动态规划)cf Educational Codeforces Round 103 (Rated for Div. 2) D. Journey

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

There are n+1 cities, numbered from 0 to n. n roads connect these cities, the i-th road connects cities i−1 and i (i∈[1,n]).

Each road has a direction. The directions are given by a string of n characters such that each character is either L or R. If the i-th character is L, it means that the i-th road initially goes from the city i to the city i−1; otherwise it goes from the city i−1 to the city i.

A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i. e., if a road is directed from city i to the city i+1, it is possible to travel from i to i+1, but not from i+1 to i. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to.

The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city i, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city i.

Input

The first line contains one integer t(1≤t≤104) — the number of test cases.

Each test case consists of two lines. The first line contains one integer n(1≤n≤3⋅105). The second line contains the string s consisting of exactly n characters, each character is either L or R.

It is guaranteed that the sum of n over all test cases does not exceed 3⋅105

Output

For each test case, print n+1 integers. The i-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the i-th city.

Input

2
6
LRRRLL
3
LRL

Output

1 3 2 3 1 3 2
1 4 1 4

思路:
只要计算城市能到达的最左边城市和最右边城市即可(一直往左走直到走不动为止,右边同理)。
因为每走一次会更改路的方向(每走两次路的方向就变成原来的方向)
所以走左边的动态方程:
当前城市可以走到距离这个城市左边的第2个城市时,pre[ i ] = pre[ i - 2 ] + 2
否则,当前城市可以走到距离这个城市左边的第1个城市时,pre[ i ] = 1
否则,pre[ i ] = 0
走右边的动态方程同理。

ps:要初始化最开始的两个城市的能到达的最左边城市数量和最末尾的两个城市的能到达的最右边城市数量。

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 3e5 + 10;
int pre[MAXN], suc[MAXN];
char a[MAXN];
int main(){
//    freopen("_in.txt", "r", stdin);
//    freopen("_out1.txt", "w", stdout);
	int t, n;
	scanf("%d", &t);
	while (t--){
	 	scanf("%d", &n);
	 	scanf(" %s", a);
	 	pre[0] = 0;
	 	pre[1] = (a[0] == 'L') ? 1 : 0;
		for (int i = 2; i <= n; i++){
			if (a[i-1] == 'L'){
				if (a[i-2] == 'R')
					pre[i] = pre[i-2] + 2;
				else
					pre[i] = 1;
			}
			else
				pre[i] = 0;
		}
		suc[n] = 0;
		suc[n-1] = (a[n-1] == 'R') ? 1 : 0;
		for (int i = n - 2; i >= 0; i--){
			if (a[i] == 'R'){
				if (a[i+1] == 'L')
					suc[i] = suc[i+2] + 2;
				else
					suc[i] = 1;
			}
			else
				suc[i] = 0;
		}
		for (int i = 0; i <= n; i++)
			printf("%d ", pre[i] + suc[i] + 1);
		printf("\n");
	 }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值