Journey(线性dp、并查集)

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

Each road has a direction. The directions are given by a string of n n 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 i i to the city i − 1 i−1 i1; otherwise it goes from the city i − 1 i−1 i1 to the city i i 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 i+1 i+1, it is possible to travel from i i i to i + 1 i+1 i+1, but not from i + 1 i+1 i+1 to i i 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 i i, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city i i i.

Input

The first line contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) — the number of test cases.

Each test case consists of two lines. The first line contains one integer n n n ( 1 ≤ n ≤ 3 ⋅ 1 0 5 ) (1≤n≤3⋅10^5) (1n3105). 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 ⋅ 1 0 5 3⋅10^5 3105.

Output

For each test case, print n + 1 n+1 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.

Example

input

2
6
LRRRLL
3
LRL

output

1 3 2 3 1 3 2
1 4 1 4

dp思路

首先想到的是动态规划, L [ i ] [ 0 ] L[i][0] L[i][0] 表示第奇数天到达城市i,可以向左访问的城市数, R [ i ] [ 1 ] R[i][1] R[i][1] 表示偶数天到达城市i,可以向右访问的城市数。所求答案为 L [ i ] [ 0 ] + R [ i ] [ 0 ] − 1 L[i][0]+R[i][0]-1 L[i][0]+R[i][0]1。状态转移方程为:
L [ i ] [ 0 ] = p [ i ] = = ′ L ′   ?   1 + L [ i − 1 ] [ 1 ] : 1 ; L [ i ] [ 1 ] = p [ i ] = = ′ R ′   ?   1 + L [ i − 1 ] [ 0 ] : 1 ; R [ i ] [ 0 ] = p [ i + 1 ] = = ′ R ′   ?   1 + R [ i + 1 ] [ 1 ] : 1 ; R [ i ] [ 1 ] = p [ i + 1 ] = = ′ L ′   ?   1 + R [ i + 1 ] [ 0 ] : 1 ; L[i][0]=p[i]=='L'~?~1+L[i-1][1]:1;\\ L[i][1]=p[i]=='R'~?~1+L[i-1][0]:1;\\ R[i][0]=p[i+1]=='R'~?~1+R[i+1][1]:1;\\ R[i][1]=p[i+1]=='L'~?~1+R[i+1][0]:1; L[i][0]=p[i]==L ? 1+L[i1][1]:1;L[i][1]=p[i]==R ? 1+L[i1][0]:1;R[i][0]=p[i+1]==R ? 1+R[i+1][1]:1;R[i][1]=p[i+1]==L ? 1+R[i+1][0]:1;
时间复杂度为 O ( n ) O(n) O(n)

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
string p; int T,n,L[N][2],R[N][2];
 
void solve(){
	cin>>n>>p; p.insert(0," ");
	for(int i=0;i<=n;i++){
		L[i][0]=p[i]=='L'?1+L[i-1][1]:1;
		L[i][1]=p[i]=='R'?1+L[i-1][0]:1;
	}
	for(int i=n;i>=0;i--){
		R[i][0]=p[i+1]=='R'?1+R[i+1][1]:1;
		R[i][1]=p[i+1]=='L'?1+R[i+1][0]:1;
	}
	for(int i=0;i<=n;i++) cout<<L[i][0]+R[i][0]-1<<" \n"[i==n];
}
 
int main(){
	ios::sync_with_stdio(false);
	for(cin>>T;T;T--) solve();
}

并查集思路

STD 用了图论的概念,将 n + 1 n+1 n+1 个城市拆分为 2 n + 2 2n+2 2n+2 个结点,分别代表奇数天数和偶数天数到达这座城市,然后根据路径添加边,计算每个结点连通块的大小。

第一个样例最终构建的图如下,答案为奇数天,每个城市的连通块的大小。

在这里插入图片描述
可以用拓展域并查集来实现这个构思,使用路径压缩与按秩合并,复杂度接近线性。

#include<bits/stdc++.h>
#define H(x) ((x)+(n+1))
using namespace std;
const int N=6e5+10;
string p; int T,n,fa[N];

int get(int x){ return fa[x]<0?x:fa[x]=get(fa[x]); }

void merge(int a,int b){
	int x=get(a),y=get(b);
	if(fa[x]<fa[y]) fa[x]+=fa[y],fa[y]=x;
	else fa[y]+=fa[x],fa[x]=y;
}

void solve(){
	cin>>n>>p;	
	for(int i=0;i<=((n+1)<<1);i++) fa[i]=-1;
	for(int i=0;i<n;i++)
		if(p[i]=='L') merge(i+1,H(i));
		else merge(i,H(i+1));
	for(int i=0;i<=n;i++)
		cout<<-fa[get(i)]<<" \n"[i==n];
}

int main(){
	ios::sync_with_stdio(false);
	for(cin>>T;T;T--) solve();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_51864047

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值