Codeforces 598B Queries on a String 【水题】
2015-11-15 18:27
483 查看
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
output
Note
The sample is described in problem statement.
题意:给定一个字符串s,下标从1 - |s|。
有m次操作,L R k - 表示讲[L, R]里面的字符右移k次,第R位右移一次到达第L位。问你经过m次变化后的字符串。
思路:优化区间长度k % (R-L+1)。先从后向前扫一遍,求出当前位置i经过k次变化后到达的位置。最后从前向后扫一遍,更新字符即可。时间复杂度O(m|s|)。
AC代码:
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.
题意:给定一个字符串s,下标从1 - |s|。
有m次操作,L R k - 表示讲[L, R]里面的字符右移k次,第R位右移一次到达第L位。问你经过m次变化后的字符串。
思路:优化区间长度k % (R-L+1)。先从后向前扫一遍,求出当前位置i经过k次变化后到达的位置。最后从前向后扫一遍,更新字符即可。时间复杂度O(m|s|)。
AC代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <vector> #define INF 0x3f3f3f3f #define eps 1e-4 #define MAXN (10000+10) #define MAXM (1000000) #define Ri(a) scanf("%d", &a) #define Rl(a) scanf("%lld", &a) #define Rf(a) scanf("%lf", &a) #define Rs(a) scanf("%s", a) #define Pi(a) printf("%d\n", (a)) #define Pf(a) printf("%lf\n", (a)) #define Pl(a) printf("%lld\n", (a)) #define Ps(a) printf("%s\n", (a)) #define W(a) while(a--) #define CLR(a, b) memset(a, (b), sizeof(a)) #define MOD 100000007 #define LL long long #define lson o<<1, l, mid #define rson o<<1|1, mid+1, r #define ll o<<1 #define rr o<<1|1 using namespace std; char str[MAXN], Cstr[MAXN]; bool vis[MAXN]; int main() { Rs(str); int m; Ri(m); while(m--) { int l, r, k; Ri(l); Ri(r); Ri(k); int len = (r - l + 1); k %= len; CLR(vis, false); for(int i = r-1; i >= l-1; i--) { int pos = (i - l + 1 + k) % len + l-1; Cstr[pos] = str[i]; } for(int i = l-1; i <= r-1; i++) str[i] = Cstr[i]; } Ps(str); return 0; }
相关文章推荐
- UINavigationBar自定义返回按钮的设置
- UINavigationBar的使用入门学习
- 简约至上 交互设计四策略 读书心得
- (转) iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController
- (转)iOS学习之UINavigationController详解与使用(三)ToolBar
- (转)IOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
- HDU 3836:Equivalent Sets【强连通】
- UI调试工具Reveal的简单用法,下载地址
- UI之CALayer详解
- UI之CALayer详解
- UIVIEW如何设置圆角
- UIImageView的属性之animationImages详解
- UIImage应用与内存管理
- 使用UISegmentControl改变画线颜色
- UI常用控件之UISegmentControl
- UI之事件与手势详解
- ios的手势操作之UIGestureRecognizer浅析
- mysql提示Column count doesn't match value count at row 1错误
- UIVIew详解
- HDU 2767:Proving Equivalences【强连通】