You are given a string s and should processm queries. Each query is described by two 1-based indicesli,ri and integerki. It means that you should cyclically shift the substrings[li...ri]ki times. The queries should be processed one after another in the order they are given.
One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.
For example, if the string s is abacaba and the query is l1 = 3, r1 = 6, k1 = 1 then the answer is abbacaa. If after that we would process the queryl2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.
The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where|s| stands for the length ofs. It contains only lowercase English letters.
Second line contains a single integer m (1 ≤ m ≤ 300) — the number of queries.
The i-th of the next m lines contains three integers li,ri andki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.
Print the resulting string s after processing allm queries.
abacaba 2 3 6 1 1 4 2
baabcaa
题意:给出一个字符串(1<=长度<=10000),再给出m进行m次查询,接下来m行,每一行三个数L,r,k,L--r表示将字符串这个区间内的字符当成一个环来逆时针转 k 次,得到一个新字符串,以后每次查询修改,都是在此字符串基础上进行的。
思路:先用一个字符串临时存放(L--r)的字符,因为下标是从0开始,所以是(src[L-1]到src[r-1])位字符,再按每次查询修改k次进行修改因是环,所以位置 i --> (i+k)%(r-L+1),再按其在临时串中位置与原串位置的对关系(tmp[0]--src[L-1]即tmp[i]--src[L-1+i])直接修改原串,这样方便些,不然直接在tmp串上修改不好操作。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> typedef long long ll; using namespace std; int main() { #ifdef OFFLINE freopen("t.txt","r", stdin); #endif ll m, i, len, L, r, k; char src[11000], tmp[11000]; scanf("%s", src); scanf("%lld", &m); while(m--){ scanf("%lld %lld %lld", &L, &r, &k); len=0; for(i=L-1; i<=r-1;i++) tmp[len++] = src[i];//len刚好记录了tmp字符串长度 for(i=0;i<len;i++){ src[L-1+((i+k)%len)] = tmp[i]; } } printf("%s\n", src); return 0; }