链接:https://ac.nowcoder.com/acm/contest/5668/B
来源:牛客网

题目描述:

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.

输入描述:

There are Q+2 lines in the input. The first line of the input contains the string S. The second line contains the integer Q. The following Q lines each denotes an operation. You need to follow the order in the input when performing those operations.

Each operation in the input is represented by a character c and an integer x. If c = 'M', this operation is a modify operation, that is, to rearrange S according to the value of x; if c = 'A', this operation is an answer operation, to answer what the x-th letter in the current string S is.

•  2≤∣S∣≤2×1e6 (|S| stands for the length of the string S)
•  S consists of lower case letters
•  1≤Q≤8×1e5
•  c = 'M' or 'A'
•  If c = 'M', 1≤∣x∣<∣S∣
•  If c = 'A', 1≤x≤∣S∣
•  There is at least one operation in the input satisfies c = 'A'

输出描述:

For each answer operation, please output a letter in a separate line representing the answer to the operation. The order of the output should match the order of the operations in the input.

solution:

题意:给你一个字符串,q次询问,如果字符为M,给的数字为正数,那么从左往右移x为,反之向左移。
把字符串想象成环,每次记录相对位置,就可以输出相对位置下的字符

#include<iostream>
#include<vector>
#include<string.h>
#include<stack>
#include<cstdio>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
char s[2000005],ch[5];
int n,x,now=0,len;

int main()
{
    scanf("%s%d",s,&n);
    len=strlen(s);
    while(n--)
    {
        scanf("%s%d",ch,&x);
        if(ch[0]=='A')
        {
            int qaq=((now+x-1)%len+len)%len;
            printf("%c\n",s[qaq]);
        }
        else
        {
            now+=x;
        }
    }

    return 0;
}