Educational Codeforces Round 1 B.Queries on a String(模拟)

Educational Codeforces Round 1B:http://codeforces.com/contest/598/problem/B

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.

Sample test(s)
input
abacaba
2
3 6 1
1 4 2
output
baabcaa


题目大意:给定一个字符串,经过m次移动(移动方式为:在区间[l,r]内(下标从1开始),向右移动1个字符,执行k次),然后输出它。

简单的模拟题,只需要注意k对(r-l+1)取模即可。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int len,l,r,k,n,t;
char s[100005],tmp[100005];

int main() {
	while(1==scanf("%s",s)) {
        len=strlen(s);
        scanf("%d",&n);
        while(n--) {
            scanf("%d%d%d",&l,&r,&k);
            --l,--r;
            k=k%(t=r-l+1);//优化,防止超时;移动(r-l+1)次是一次循环,只有k%(r-l+1)次的移动是有效的
            k=t-k;//转换为向左移动
            if(k) {
                strncpy(tmp,s+l,t);//复制移动的区间
                strncpy(s+l,tmp+k,t-k);//将区间内的后t-k个字符移到区间前面
                strncpy(s+l+t-k,tmp,k);//将区间内的前k个字符移到区间后面
                /*
                    看别人的代码时,发现一个stl的库函数:rotate(first,middle,last),可以实现元素的左移
                    rotate(s+l,s+l+k,s+r+1);即可实现上述操作

                    还有用string的话也很简单:
                    str=str.substr(0,l)+str.substr(l+t-k,t-k)+str.substr(l,k)+str.substr(r+1);
                */
            }
        }
        printf("%s\n",s);
	}
	return 0;
}


你可能感兴趣的:(模拟,codeforces)