codeforces - Queries on a String(模拟+取余)

来源:互联网 发布:miaomiaoav永久域名 编辑:程序博客网 时间:2024/06/06 08:23
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
abacaba23 6 11 4 2
output
baabcaa
Note

The sample is described in problem statement.


这题就是从一个区间段每个字符向前移多少位的问题,一开始都是用%来进行运算,所以总是Wa,Wa了两次后突然发现如果是都取余的话,就会造成能被整除的都会受到影响,所以想到了其实只要到达最大值时取余一次就行了,因为在区间内能达到最大的只有一次,所以只要后面都是相加就行了。。


AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<cstdio>#include<cmath>using namespace std;#define CRL(a) memset(a,0,sizeof(a))typedef unsigned __int64 ll;#define T 10005#define mod 1000000007int main(){#ifdef zsc    freopen("input.txt","r",stdin);#endif    int n,L,R,k;    string s;    while(cin >> s)    {        cin >> n;        while(n--)        {           cin >> L >> R >> k;           int mid = R-L+1;           k%=mid;           if(!k)continue;           char tmp[T];          int temp = (L-1+k)%R;           for(int i=L-1;i<R;++i){                if(temp%R==0)temp = L-1;              tmp[temp++] = s[i];           }           for(int i=L-1;i<R;++i){              s[i] = tmp[i];           }        }        cout << s << endl;    }    return 0;}


0 0
原创粉丝点击