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

题目传送
题意:
给你一个字符串,现在给你n个操作,如果输入的操作是A,那么输出现在的第x个位置的字符。如果是M,如果x为正,那么现在字符串的最左边的x个字符移到末尾,如果x为负,那么把最右边的x个字符串移到左边。

思路:
我们只需要记录现在的字符串的开头位置是哪个位置就行了
比如:
abcdefg
现在的开头位置是0,如果现在我们现在把最左边的3个字符移到右边,那么现在的开头位置就是d,那么0 + 3就是现在的开头位置,s[3]。

那么现在如果我们要查询第3个位置,那么就是在现在的开头位置的基础上加上3,就是3 + 3 - 1,s[5] = f

但是我们现在还要面对越界的问题,怎么防止越界呢?,就是取模操作,但是取模会不会造成错误呢?答案是不会的,因为我们进行取模其实就是把他当成一个环来处理。

AC代码

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
//#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
//#define Mem(a,b) memset(a,b,sizeof(a))
//#define lowbit(x) (x)&(-x)
//const int N = 2e5 + 10;
//const long long INFINF = 0x7f7f7f7f7f7f7f;
//const int INF = 0x3f3f3f3f;
//const double EPS = 1e-7;
//const int mod = 1e9+7;
//const double II = acos(-1);
//const double PP = (II*1.0)/(180.00);
//typedef long long ll;
//typedef unsigned long long ull;
//typedef pair<int,int> pii;
//typedef pair<ll,ll> piil;
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    string s;
    cin >> s;
    int n,sum = 0,len = s.size();
    cin >> n;
    while(n--)
    {
        char c;
        int x;
        cin >> c >> x;
        if(c == 'A')
            cout << s[(sum+x-1+len)%len] << endl;
        else
            sum = (sum + x + len)%len;
    }
}

猜你喜欢

转载自blog.csdn.net/moasad/article/details/107436944