关闭

Codeforces 598B Queries on a String 【思维】

标签: codeforces字符串
285人阅读 评论(0) 收藏 举报
分类:
B. 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 liri 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 liri 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.


/*
    题意:给你一个字符串和m次查询,每次查询给你l,r,k
          表示在区间[l,r]内依次把最后的元素移到最前面k次,
          求m次查询以后的字符串
    类型:字符串
    分析:先把k%(R-L+1),得到有效移动次数,再从后向前扫一遍,
          求出当前位置i经过k次变化后到达的位置.
          最后从前向后扫一遍,更新字符.
*/

#include<cstdio>
#include<iostream>
using namespace std;
char s[10005],t[10005];
int m;
int main()
{
    cin>>s;
    cin>>m;
    while(m--){
        int l,r,k;
        cin>>l>>r>>k;
        int len=(r-l+1);
        k%=len;
        for(int i=r-1;i>=l-1;i--){
            int pos=(i-(l-1)+k)%len+l-1;
            t[pos]=s[i];
        }
        for(int i=l-1;i<=r-1;i++){
            s[i]=t[i];
        }
    }
    cout<<s<<endl;
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:31242次
    • 积分:1586
    • 等级:
    • 排名:千里之外
    • 原创:130篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    最新评论