Educational Codeforces Round 103 (Rated for Div. 2)D. Journey(dp)

题目描述

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.

Example

input
2
6
LRRRLL
3
LRL
output
1 3 2 3 1 3 2
1 4 1 4

题目大意

给出 n 条边,有 n+1 个不同的城镇,每一条边都有一个方向:
L 表示从 i 到 i-1 有一条边
R表示从 i 到 i+1 有一条边
旅游者从每一个城镇 i 出发,每到达下一个城镇时,所有的边都会反转,L 变为 R,R 变为 L,问对于每个城镇而言,旅行者可以到达的城镇数目最多是多少 。

题目分析

对于每个答案,我们可以将其分成三部分:
1)起点城市本身:1。

2)往左走能够到达的城市数量pre[i],因为每走一步都会使所有边反转,因此pre[i]=从i开始向左LRLR序列的最大长度
我们可以思考一下pre[i]如何求得:其实就是一个简单的dp
如果s[i]='L’并且s[i-1]=‘R’,那么pre[i]=pre[i-2]+2;
如果s[i]=‘L’,那么pre[i]=1;
否则,pre[i]=0;

3)往右走能够到达的城市数量suf[i],同理suf[i]=从i+1开始向右RLRL序列的最大长度
求解suf[i]的流程与求解pre[i]的思路是一样的,这里就不多说了。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <bitset>
#include <algorithm>
#define LL long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=3e5+5;
char s[N];
int pre[N],suf[N];
int main()
{
    
    
    int t;
	scanf("%d",&t);
	while(t--)
	{
    
    
		int n;
		scanf("%d",&n);
		scanf("%s",s+1);
		for(int i=0;i<=n+1;i++) pre[i]=suf[i]=0;		//清空pre[]和suf[]
		for(int i=1;i<=n;i++)				//求出pre[]
		{
    
    
			if(s[i]=='L'&&s[i-1]=='R') pre[i]=pre[i-2]+2;
			else if(s[i]=='L') pre[i]=1;
		}
		for(int i=n-1;i>=0;i--)				//求出suf[]
		{
    
    
			if(s[i+1]=='R'&&s[i+2]=='L') suf[i]=suf[i+2]+2;
			else if(s[i+1]=='R') suf[i]=1;
		}
		for(int i=0;i<=n;i++)				//第i个城市能到多少点即为三部分内容的相加
			printf("%d ",pre[i]+suf[i]+1);
		puts("");
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/113432840