2020牛客多校第三场B题Classical String Problem(思维)

题目链接 https://ac.nowcoder.com/acm/contest/5668/B

题意:输入一个字符串,M操作表示把前面t个字符移到后面,如果t<0,那就是把后面t个字符移到前面,A操作 就是问你第t个字符是什么输出。

题解:把字符串看做一个环,收尾相接,无论进行多少次操作字符串的顺序始终没变。这样就不用修改字符串,只需要一个变量表示当前的第一个字符位置,答案就能O(1)输出。

#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
const ll mod =1e9+7;
char s[maxn],a[maxn];
int main(){
    IOS
    int n,t,len;
    scanf("%s",s);
    scanf("%d",&n);
    len=strlen(s);
    for(int i=0;i<n;i++){
        scanf("%s",a);
        scanf("%d",&t);
        if(a[0]=='A') printf("%c\n",s[t-1]);
        else {
            if(t<0){
                rotate(s,s+len+t,s+len);
            }else 
                rotate(s,s+t,s+len);
        }
    }

    return 0;
}
TEL
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=2e6+7;
const ll mod =1e9+7;
char s[maxn];
char a[2];
int main(){
    IOS
    int n,t,len;
    scanf("%s",s);
    scanf("%d",&n);
    len=strlen(s);
    int now=0;
    for(int i=0;i<n;i++){
        scanf("%s",a);
        scanf("%d",&t);
        if(a[0]=='A') printf("%c\n",s[ (now+t-1+len)%len]);
        else {
            now=(now+t+len)%len;
        }
    }

    return 0;
}

 

posted @ 2020-08-05 16:54  杰瑞与汤姆  阅读(136)  评论(0编辑  收藏  举报