【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题

原创 2015年11月19日 09:16:32

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 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.

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

The sample is described in problem statement.



#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=10010,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
char s[N];
char tmp[N];
int main()
{
	while(~scanf("%s",s+1))
	{
		int m;scanf("%d",&m);
		int l,r,k;
		while(m--)
		{
			scanf("%d%d%d",&l,&r,&k);
			k%=(r-l+1);
			int g=0;
			for(int i=r-(k-1);i<=r;i++)tmp[++g]=s[i];
			for(int i=l;i<=r-k;i++)tmp[++g]=s[i];
			for(int i=1,j=l;i<=g;i++,j++)s[j]=tmp[i];
		}
		puts(s+1);
	}
	return 0;
}
/*
【题意】
给你一个长度在[1,10000]范围内的字符串,让你执行m次操作
对于每个操作,我们要对这个字符串[l,r]区间范围的数,向右做旋转式平移k次
(注,向右的旋转式平移就是位置p是由位置p-1的数转移得,位置l是由位置r转移得)
让你输出所有操作完成之后的串。

(1<=m<=300,1<=l<=r<=|s|,1<=k<=1e6)

【类型】
字符串操作水题

【分析】
首先k很大,但是显然可以使得k%=(r-l+1),提高运行效率。
然后我们把右移溢出的k个字符先提取过来——
for(int i=r-(k-1);i<=r;i++)tmp[++g]=s[i];
然后再把右移无溢出的剩余字符提取过来——
for(int i=l;i<=r-k;i++)tmp[++g]=s[i];
接着还原即可——
for(int i=1,j=l;i<=g;i++,j++)s[j]=tmp[i];

【时间复杂度&&优化】
O(m*|s|)

*/


版权声明:题解中哪里写错请一定要指出来QwQ 转载还请注明下出处哦,谢谢^_^ 举报

相关文章推荐

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

题目大意:给定一个字符串,经过m次移动(移动方式为:在区间[l,r]内(下标从1开始),向右移动1个字符,执行k次),然后输出它。 简单的模拟题,只需要注意k对(r-l+1)取模即可。

【Codeforces Round 336 (Div 2) B】【水题 前缀和】Hamming Distance Sum 双字符串所有位置匹配下的差异数之和

B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabyte...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

【Educational Codeforces Round 3 B】【水题 基本容斥】The Best Gift n本书任选两个不能同一类型的方案数

B. The Best Gift time limit per test 2 seconds memory limit per test 256 megabytes ...

Educational Codeforces Round 9 B. Alice, Bob, Two Teams【水题】

B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megabytes i...

Codeforces Round #397(Div. 1 + Div. 2 combined)B. Code obfuscation【水题】

B. Code obfuscation time limit per test 2 seconds memory limit per test 512 megabytes inp...

【Codeforces Round 339 (Div 2)B】【水题】Gena's Code 若干10数+1特殊数的乘积

B. Gena's Code time limit per test 0.5 seconds memory limit per test 256 megabytes ...

Codeforces - 748B Santa Claus and Keyboard Check(字符串水题)

点击打开题目链接 B. Santa Claus and Keyboard Check time limit per test 2 seconds memory limi...

Educational Codeforces Round 13——D. Iterated Linear Function(矩阵快速幂或普通快速幂水题)

D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes i...

【Educational Codeforces Round 3 A】【水题 排序】USB Flash Drives 最少数量U盘存储m大小文件

A. USB Flash Drives time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) 831C. Jury Marks【二分】【水题】

C. Jury Marks time limit per test 2 seconds memory limit per test 256 megabytes inp...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)