牛客多校(2020第三场)B Classical String Problem
题目链接:https://ac.nowcoder.com/acm/contest/5668/B
题意:
有一个字符串,有俩种操作
- 询问第x个字符
- 把最左边的x个字符搬到最右边或者把最右边的x个字符搬到最左边
题解:
- 用一个指针k,表示字符串起始位置,向右移动则(k + x) % length, 像左移动则(k + x + length) % length
- 查询则s[(k+x-1)%length]
1 #include<stdio.h>
2 #include<iostream>
3 #include<math.h>
4 #include<string.h>
5 #include <algorithm>
6 #include<time.h>
7 #include <numeric>
8 #include <vector>
9 #include <functional>
10 #include <list>
11 #include <ctype.h>
12 #include<map>
13 #include <set>
14 #define N 1000005
15 using namespace std;
16 string s;
17
18 int main(){
19 cin>>s;
20 int n, k = 0;
21 scanf("%d",&n);
22 getchar();
23 while(n--){
24 char a;
25 int x;
26 scanf("%c %d",&a,&x);
27 getchar();
28 if(a=='M') {
29 k = (k + x + s.length()) % s.length();
30 }
31 else {
32 printf("%c\n",s[(k+x-1) % s.length()]);
33 }
34
35 }
36 }

浙公网安备 33010602011771号