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

传送门


挺有意思的一道题,拿来写篇博客吧

我们大概模拟一下,假如只考虑将前面部分移动到后面,如 n o w c o d e r nowcoder nowcoder的前四位移动到最后,那么为 o d e r n o w c odernowc odernowc,那么我们查询第三个位置,实际上就是查询源串的第 3 + 4 3+4 3+4个位置;如果是移动后缀呢,比如 n o w c o d e r nowcoder nowcoder的后三位移动到前面 d e r n o w c o dernowco dernowco,那么查询第四位时,实际上查询的是 ( 4 + 5 ) % 8 − 1 (4+5)\%8-1 (4+5)%81;如果中间穿插移动的话,只需要维护一个增量,然后每次查询 ( ( x + d e l t ) % l e n − 1 + l e n ) % l e n ((x+delt)\%len-1+len)\%len ((x+delt)%len1+len)%len

这个想法的正确性是,先将字符串首尾相连看做循环队列,初始时指针在第一个字符的位置,然后每次移动的前缀后缀相当于指针的移动

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const double dinf=1e300;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e6+10;

char s[maxn];

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int t,x;
    char op;
    scanf("%s",s);
    scanf("%d",&t);
    ll dlt=0;
    int n=strlen(s);
    while(t--){
        getchar();
        scanf("%c%d",&op,&x);
        if(op=='A'){
            int sz=(x+dlt)%n-1;
            if(sz<0) sz+=n;
            printf("%c\n",s[sz]);
        }else{
            if(x>0){
                dlt+=x;
            }else{
                dlt+=n-abs(x);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值