2020牛客暑期多校训练营(第三场)B.Classical String Problem(思维)

地址:https://ac.nowcoder.com/acm/contest/5668/B

题意:

给定字符串s

n次操作

M: x 

x>0,将左边x个字符般到右边。x<0,将右边数x个字符搬到左边

A: x  询问当前第x个字符

解析:

思路:维护变化后的第一个字符,在原字符里出现的位置

上样例:

cnt作为此指针

0  1  2  3  4  5  6  7

n  o  w   c    o   d  e  r  cnt=0

o  d  e  r  n   o  w   c      cnt=4,此状态下,o处于原字符串的i=4位置

o  w  c   o  d  e   r  n  cnt=1,此状态下,w前的那个o,处于原字符串的i=1位置

..........

知道维护变化后的第一个字符,在原字符里出现的位置,就很容易得出询问的答案了。

#include<iostream>
using namespace std;
const int maxn=2e6+10;
int a[maxn];
string s;
int main()
{
    cin>>s;
    int len=s.size();
    int n;
    int cnt=0;
    cin>>n;
    while(n--)
    {
        int x;
        char ch[4];
        scanf("%s",ch);
        scanf("%d",&x);
        if(ch[0]=='M')
        {
            cnt=(cnt+x)%len;
        }
        else
        {
            x--;
            int md=(cnt+x+len)%len;
            printf("%c\n",s[md]);
        }
    }
}

 

posted @ 2020-07-18 23:18  liyexin  阅读(116)  评论(0编辑  收藏  举报