题目描述
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≤∣S∣≤2×1062≤∣S∣≤2×106 (|S| stands for the length of the string S)
• S consists of lower case letters
• 1≤Q≤8×1051≤Q≤8×105
• c = 'M' or 'A'
• If c = 'M', 1≤∣x∣<∣S∣1≤∣x∣<∣S∣
• If c = 'A', 1≤x≤∣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'.
我非常老实的移动字符串,然后就超时了
超时代码
#include<iostream>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<vector>
#include<queue>
#include<array>
#include <stdlib.h>
#include <stdio.h>
#include<cstdio>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
//const int mod=10^9+7;
#define inf 0x3f3f3f3f
//tuple<int,string,int>p[55];
//pair<int,string>male[55],female[55];
int main()
{
char s[2000100];
int q;
scanf("%s",s);
cin>>q;
while(q>0)
{
char c;
int x;
cin>>c>>x;
int len=strlen(s);
if(c=='A')
{
cout<<s[x-1]<<endl;
}
else
{
if(x>0)
{
char t[2000100];
memcpy(t+len-x,s,x);
memcpy(t,s+len-x,len-x);
t[len]='\0';
strcpy(s,t);
}
else
{
char t[2000100];
x=-x;
memcpy(t,s+len-x,x);
memcpy(t+x,s,len-x);
t[len]='\0';
strcpy(s,t);
}
}
q--;
}
return 0;
}
一开始string的长度开不到2e6+10,导致了段错误
#一个string拷贝到另一个string还要使用for()来进行拷贝
后来使用gets()导致了编译错误
#千万不要用gets()
memcpy+strcpy导致了运行错误
#memcpy拷贝结束以后不会在string末尾添加\0,不过本题错误不是这个原因
🌞ac代码
原来一点都不用移动😢
只需要定位第一个字符,在输出时使用的只有定位
const int N=2e6+5;
int t,n,x,k=0;
char s[N],c;
int main()
{
scanf("%s%d",s,&t);
int n=strlen(s);
while(t--)
{
getchar();
scanf("%c%d",&c,&x);
if(c=='M')
k=k+x;
else
printf("%c\n",s[(k+x-1>=0?(k+x-1)%n:n-abs(k+x-1)%n)]);
}
return 0;
}