B.Classical String Problem

B.Classical String Problem

题目下载

B_Classical_String_Problem.htm.zip

题解

如果将字符串S看作是首尾相接的一个环,那么M操作实际上只是不会对环造成任何影响,只是将环破成链之后的首字母的位置改变了。

例如

White(M 2)

截屏2020-07-19 下午1.39.26

看作环,马上就可以想到取模操作

只需要维护一个当前首字母的指针(ptr)即可
这题用cin,cout会T,尽量用scanf和printf(能过当我没说,反正我T了)

#include <cstdio>
#include <cstring>

#define MaxN 2000000+1
using namespace std;

char str[MaxN];
int main(){
    scanf("%s",str);
    int n = strlen(str);
    int q,x;
    int ptr = 0;
    scanf("%d",&q);
    
    char cmd;
    while (q--) {
        getchar();
        scanf("%c %d",&cmd,&x);
        if(cmd == 'M'){
            ptr = (ptr + x + n) % n;
        }else{
            x = (x - 1 + ptr) % n;
            putchar(str[x]);
            putchar('
');
        }
    }
    return 0;
}

---- suffer now and live the rest of your life as a champion ----
原文地址:https://www.cnblogs.com/popodynasty/p/13339427.html