2020牛客暑期多校训练营(第三场)B
Clam and Fish
https://ac.nowcoder.com/acm/contest/5668/A
题目描述
Given a string consists of lower case letters. You're going to perform operations one by one. Each operation can be one of the following two types.
Modify: Given an integer , you need to modify according to the value of . If is positive, move the leftmost letters in to the right side of ; otherwise, move the rightmost letters in to the left side of .
Answer: Given a positive integer . Please answer what the -th letter in the current string is.
输入描述
There are lines in the input. The first line of the input contains the string . The second line contains the integer . The following 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 and an integer . If c=='M'
, this operation is a modify operation, that is, to rearrange according to the value of ; if c=='A'
, this operation is an answer operation, to answer what the -th letter in the current string is.
( stands for the length of the string ).
consists of lower case letters.
.
or . If , . If , .
There is at least one operation in the input satisfies .
输出描述
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.
示例1
输入
nowcoder 6 A 1 M 4 A 6 M -3 M 1 A 1
输出
n o w
备注
Initially, is "nowcoder", six operations follow.
The -st operation is asking what the -st letter is. The answer is 'n'.
The -nd operation is to move the leftmost letters to the rightmost side, so is modified to "odernowc".
The -rd operation is asking what the -th letter is. The answer is 'o'.
The -th operation is to move the rightmost letters to the leftmost side, so is modified to "owcodern".
The -th operation is to move the leftmost letter to the rightmost side, so is modified to "wcoderno".
The -th operation is asking what the -st letter is. The answer is 'w'.
分析
将字符串看作一个环,定义一个指针 指向字符串的起点,从起点开始逆时针遍历整个环,得到的就是当前字符串;不论如何更改,环的结构是不会变的。查询时,从起点开始,逆时针经过 个点,最终到达的点即为询问的答案;更新时,若 ,要将字符串末尾的某个字符作为起点,将指针顺时针移动 次;否则,将指针逆时针移动 次。环上的移动,通过对字符串长度 取模来体现。
代码
/****************************************************************** Copyright: 11D_Beyonder All Rights Reserved Author: 11D_Beyonder Problem ID: 2020牛客暑期多校训练营(第三场) Problem B Date: 8/14/2020 Description: Pointer Simulation *******************************************************************/ #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int N=2000005; int _; char s[N]; int q; int len; int main(){ scanf("%s%d",s,&q); len=strlen(s); int pointer=0; while(q--){ char c; int x; getchar(); scanf("%c%d",&c,&x); if(c=='M'){ //取模体现环上移动的性质 pointer=(pointer+x+len)%len; }else{ printf("%c\n",s[(pointer+x-1)%len]); } } return 0; }
收集牛客暑期多校训练营的题解