跳到正文

个人记录

小信下棋

jinruitao约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年4月10日
更新
2026年4月10日
篇幅
约 2 分钟

版本

  1. v1当前

时间:1s 空间:256M

题目描述:

小友给了小信一个长度为n的只包含”L”和 的”R”字符串移动序列S。 对应的,小信有一个1×n的棋盘,一开始每个格子上都放着一颗棋子。 每一轮移动,每个格子上的棋子按照它当前位置对应移动序列的字母来决定向左还是向右移动,“L”则向左移动一格,“R”则向右移动一格。 请输出经过 10^100 轮移动后每个格子上棋子的个数。

输入格式:

第一行包含一个字符串S。

输出格式:

输出一行n个整数,表示经过 10100 轮移动后每个格子上棋子的个数。

样例1输入:

RRLRL

样例1输出:

0 1 2 1 1

样例2输入:

RRRLLRLLRRRLLLLL

样例2输出:

0 0 3 2 0 2 1 0 0 0 4 4 0 0 0 0

约定:

对于100%的数据,2≤|S|≤105。保证S开头为 ”R“ ,末尾为“L”。

思路:

10^100……找规律的题 R R L R L 1 1 1 1 1 0 1 2 1 1 数字都在LR交界处且一定是R在左

标程

C++
#include<bits/stdc++.h>
using namespace std;
char s[100010];
int a[100010];
stack <int> st;
int main() {
	cin>>s;
	//cout<<s;
	int n=strlen(s+1);
	//cout<<n;
	for(int i=0;i<=strlen(s+1);i++){
		if(s[i]=='R')
			st.push(1);
		else{
			a[i-1]+=(st.size()+1)/2;
			a[i]+=st.size()-(st.size()+1)/2;
			while(st.size())st.pop();
		}
	}
	while(st.size())st.pop();
	for(int i=strlen(s+1);i>=0;i--){
		if(s[i]=='L')
			st.push(1);
		else{
			a[i+1]+=(st.size()+1)/2;
			a[i]+=st.size()-(st.size()+1)/2;
			while(st.size())st.pop();
		}
	}
	for(int i=0;i<=strlen(s+1);i++)cout<<a[i]<<" ";
	return 0;
}
原文