牛客(多校3):Classical String Problem

链接:https://ac.nowcoder.com/acm/contest/5668/B
来源:牛客网

题目描述

Given a string S consists of lower case letters. You’re going to perform Q operations one by one. Each operation can be one of the following two types:

Modify: Given an integer x. You need to modify S according to the value of x. If x is positive, move the leftmost x letters in S to the right side of S; otherwise, move the rightmost |x| letters in S to the left side of S.
Answer: Given a positive integer x. Please answer what the x-th letter in the current string S is.
输入描述:
There are Q+2 lines in the input. The first line of the input contains the string S. The second line contains the integer Q. The following Q 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 c and an integer x. If c = ‘M’, this operation is a modify operation, that is, to rearrange S according to the value of x; if c = ‘A’, this operation is an answer operation, to answer what the x-th letter in the current string S is.

•  2 \le |S| \le 2 \times 10^62≤∣S∣≤2×10 

6
(|S| stands for the length of the string S)
• S consists of lower case letters
• 1 \le Q \le 8 \times 10^51≤Q≤8×10
5

•  c = 'M' or 'A'
•  If c = 'M', 1 \le |x| < |S|1≤∣x∣<∣S∣
•  If c = 'A', 1 \le x \le |S|1≤x≤∣S∣
•  There is at least one operation in the input satisfies c = 'A'

输出描述:
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, S is ‘nowcoder’, six operations follow.

• The 1-st operation is asking what the 1-st letter is. The answer is 'n'.
• The 2-nd operation is to move the leftmost 4 letters to the rightmost side, so S is modified to 'odernowc'.
• The 3-rd operation is asking what the 6-th letter is. The answer is 'o'.
• The 4-th operation is to move the rightmost 3 letters to the leftmost side, so S is modified to 'owcodern'.
• The 5-th operation is to move the leftmost 1 letter to the rightmost side, so S is modified to 'wcoderno'.
• The 6-th operation is asking what the 1-st letter is. The answer is 'w

分析:

首先我们很容易使用图去模拟一下整个过程,
其实我们可以把字符串‘S’当成一个收尾相连的特殊字符串,其次我们考虑一下,怎样实现原数组不发生位置变化的同时使字符串S中的元素发生位置变化,说的有点扯哈,别急别急,我们都学过相对运动,假如我们把看成数组静止的,把其指向第一个元素的Num 进行移动,又因为字符串是一个环形,那么我们Num指向的可以看成初始的第一个元素,有它在我们便可以随时访问原数组了,不过需要一些计算,计算下面详细讲解。
紧接着,我们也看到了题意,输入分成了两个种,一种是A选项,主要是查第overset个元素是什么,打表。另一种是B选项,这个选项分为两种,一种是overset为负数,一种是为overset正数。
因此呢我们分开来考虑,

先考虑B选项:

```cpp
if(op[0]=='M'){
			if(overset>0)
			Num+=overset;
			else{
				Num+=(l+overset);
			}	
			Num=Num%l;
		}
```//注意:这里Num一直是指向数组的最初的首元素的。

再考虑A选项:

毕竟先A后再写B,必须将二者单独作为函数去实现,本人感觉麻烦,所以就想简便一点。

else {
long AP = (Num + overset) % l;
    if(AP==0){
        printf("%c\n" ,S[S.length()-1]);//这里你可以想一下AP=0
        //意味着什么?也就是说指向原数组首地址的坐标值+移动的位数=数组的长度,~~是不是活到头了~~ ,
        //所以是原数组最后一个元素了
    }else{
        printf("%c\n" ,S[(int)(AP-1)]);
        }
    }

总代码:

#include <bits/stdc++.h>
using namespace std;
int main(){
	string S;
	cin>>S;
	int l=S.length();
	int p,overset;
	cin>>p;
	long long Num=0;
	char op[2];
	for(int i=1;i<=p;i++){
		scanf("%s%d",op,&overset);
		if(op[0]=='M'){
			if(overset>0)
			Num+=overset;
			else{
				Num+=(l+overset);
			}	
			Num=Num%l;
		}
		
	else {
    long AP = (Num + overset) % l;
        if(AP==0){
            printf("%c\n" ,S[S.length()-1]);
        }else{
            printf("%c\n" ,S[(int)(AP-1)]);
            }
        }
			
	}
}

欢迎指正!!!!!!!!

猜你喜欢

转载自blog.csdn.net/qq_46144237/article/details/107453065
今日推荐