当先锋百科网

首页 1 2 3 4 5 6 7

(CodeForces - 598B)Queries on a String

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

You are given a string s and should process m queries. Each query is described by two 1-based indices li, ri and integer ki. It means that you should cyclically shift the substring s[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 query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.

Input

The first line of the input contains the string s (1 ≤ |s| ≤ 10 000) in its initial state, where |s| stands for the length of s. 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 and ki (1 ≤ li ≤ ri ≤ |s|, 1 ≤ ki ≤ 1 000 000) — the description of the i-th query.

Output

Print the resulting string s after processing all m queries.

Examples

Input

abacaba
2
3 6 1
1 4 2

Output

baabcaa

Note

The sample is described in problem statement.

题目大意:给出一个序号1……s的字符串s,先进行m此操作,每一次操作将区间[l,r]的字符右移k位,其中r位置右移一位到l位置,以此类推,问m次操作后的字符串。

思路:模拟操作过程即可。计算出字符串当前位置的字符经过m次变换后所到达的位置,然后从左到右输出即可,其中k可以优化成k%(l-r+1)。

#include<cstdio>
using namespace std;

const int maxn=;
char a[maxn],b[maxn];

int main()
{
    scanf("%s",a+);
    int m;
    scanf("%d",&m);
    while(m--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        int len=r-l+;
        k%=len;
        for(int i=l;i<=r;i++)
        {
            int tmp=(i-l+k)%len+l;
            b[tmp]=a[i];
        }
        for(int i=l;i<=r;i++) a[i]=b[i];
    }
    printf("%s\n",a+);
    return ;
}