2020牛客多校 第三场 B-Classical String Problem (字符串 + 指针维护)

题目链接: B-Classical String Problem

Description

题意:给出一个字符串,进行q次操作,输出A操作的结果

Given a string S consists of lower case letters. You’re going to perform Q operations one by one. Each operation can be one of the following two types:

  • Modify: Given an integer x. You need to modify S according to the value of x. If x is positive, move the leftmost x letters in S to the right side of S; otherwise, move the rightmost |x| letters in S to the left side of S.
  • Answer: Given a positive integer x. Please answer what the x-th letter in the current string S is.

Sample Input

nowcoder
6
A 1
M 4
A 6
M -3
M 1
A 1

Sample Output

n
o
w

More Info

  • 2 <= |S| <= 2 *106
  • 1 ≤ Q ≤ 8×105

Method

  • 可以把字符串想象成一个首位相接的循环串;
  • 设置一个整型变量 temp 来维护首字符所在字符数组下标;
  • 若操作为 M ,则更新 temp = (temp + s.length + x)%s.length;
  • 若操作为 A ,则直接输出 S[(temp + x - 1)%S.length];

code

详见注释

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;
#define Max 2000005
#define ll long long
//template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
char s[Max];

int main()
{
	int q, t, temp=0, len;
	
	scanf("%s%d", s, &q);
	len = strlen(s);
	getchar();
	while(q--)
	{
		char c;
		scanf("%c%d", &c, &t);
		getchar();
		//cin >> c >> t;
		//cout << t << endl;
		if(c == 'A') printf("%c\n", s[(t+temp-1)%len]);	//指针位置+移动距离 输出 
		else temp = (temp + t + len)%len;				//更新指针位置 
	}
	return 0;
}

蒟蒻一只,欢迎指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值