CodeForces 598B(循环数组)

对于循环数组的问题,就是找偏移K后位置

偏移后位置=起始位置+(相对位置+K)%(长度+1)

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define debug printf("!
")
#define INF 10000
#define MAX(a,b) a>b?a:b
#define blank pf("
")
#define LL long long

char str[10010],tmp[10010];

void change(int l,int r,int k)
{
     int i,j;
     for(i=l-1;i<r;i++)
     {
          tmp[i] = str[i];
     }
     for(i=l-1;i<r;i++)
     {
          int y =(i-l+1+k)%(r-l+1);
          str[l-1+y] = tmp[i];
     }
}

int main()
{
     while(~sf("%s",str))
     {
          int n,l,r,k;
          sf("%d",&n);
          while(n--)
          {
               sf("%d%d%d",&l,&r,&k);
               change(l,r,k);
          }
          pf("%s
",str);

     }
     return 0;
}
原文地址:https://www.cnblogs.com/qlky/p/5159711.html