第三场B

B.Classical String Problem

 

 

 

 

 

 

 

 

 示例1

输入

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

 

输出

n
o
w

 

 

 ——————————————————————————————————————————————————————————

意思:A x则是输出当前字符串第x个字符

M x则是将最左(x<0)/ 最右(x>0)的x个字符移到最右 / 最左

 

题解思路

 

想象成字符串是头尾接在一起的,只需维护一个指针指向当前第一个字符的位置,就可以O(1)的时间复杂度进行操作(指针左即为尾部,右即为头部,指针左移即为尾部移到头部,指针右移即为头部移到尾部)

以给出的例子为例

初始时指针在n前面 ——  | nowcoder

操作一询问,输出n

操作二右4个字符移到左边,相当于指针右移4个字符nowc | oder

操作三询问,输出o

操作四左3个字符移到右边,相当于指针左移3个字符n | owcoder

以此类推

 

代码

#include<cstdio>
#include<cstring>
const int SIZE = 500001;
char s[SIZE];
int main(){
    scanf("%s",s);
    int n=strlen(s);
    int Q;
    scanf("%d",&Q);
    int offset=0;
    while(Q--){
        char c[4];
        scanf("%s",c);
        if(c[0]=='M'){
            int x;
            scanf("%d",&x);
            offset=(offset+x+n)%n;//移动指针
        }
        else{
            int x;
            scanf("%d",&x);
            printf("%c\n",s[(offset+x-1)%n]);//输出以当前指针为头的第x个字符
        }
    }
    return 0;
}

 

posted @ 2020-07-28 10:08  what_the  阅读(69)  评论(0)    收藏  举报