题目链接: 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;
}
蒟蒻一只,欢迎指正