codeforces_598B. Queries on a String
2016-04-04 21:31
465 查看
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.
Examples
input
output
Note
The sample is described in problem statement.
用上k=k%(r-l+1);后直接模拟还是会超时,需要逐个位置算出其变化后的位置,这样就只要扫一遍算出位置,和扫一遍更新原字符串的位置就行。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
char c[10010];
char a[10010];
int main()
{
//FOP;
gets(c);
int m,i,j;
Si(m);
while(m--)
{
int l,r,k;
Si(l);Si(r);Si(k);
l--;r--;
if(l==r)continue;
int len=r-l;
k=k%(r-l+1);
for(i=l;i<=r;i++)
{
int ans=i-k;
if(ans<l)ans+=len+1;
a[i]=c[ans];
}
for(i=l;i<=r;i++)
c[i]=a[i];
}
Ps(c);
return 0;
}
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.
Examples
input
abacaba 2 3 6 1 1 4 2
output
baabcaa
Note
The sample is described in problem statement.
用上k=k%(r-l+1);后直接模拟还是会超时,需要逐个位置算出其变化后的位置,这样就只要扫一遍算出位置,和扫一遍更新原字符串的位置就行。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
char c[10010];
char a[10010];
int main()
{
//FOP;
gets(c);
int m,i,j;
Si(m);
while(m--)
{
int l,r,k;
Si(l);Si(r);Si(k);
l--;r--;
if(l==r)continue;
int len=r-l;
k=k%(r-l+1);
for(i=l;i<=r;i++)
{
int ans=i-k;
if(ans<l)ans+=len+1;
a[i]=c[ans];
}
for(i=l;i<=r;i++)
c[i]=a[i];
}
Ps(c);
return 0;
}
相关文章推荐
- hdu 1242 Rescue bfs+优先队列 解题报告
- UE4 技巧总结
- iOS --- 获取屏幕顶层的UIViewController
- POJ 3368 Frequent values(线段树/RMQ)
- BlueStacks如何调整屏幕分辨率和屏幕宽度
- iOS --- 理解UIWindow及其常见使用场景
- js设置cookie,为cookie中设置多个key value
- HDU 4027 Can you answer these queries?(线段树)
- LeetCode 232. Implement Queue using Stacks
- 【VK Cup 2016 - Round 1 (Div 2 Edition)E】【目标线思想差值相减 优先队列】Bear and Contribution c5成本+5 c1成本+1使得至少k人贡献相
- LeetCode 225. Implement Stack using Queues
- 设置request.setCharacterEncoding()后,servlet中文乱码问题
- IOS-- UIView中的坐标转换
- UICollectionView用法补充(照片轮播墙)
- UDID、UUID+keychain
- 在Java8的foreach()中使用return/break/continue
- HDU 3279 Nth Largest Value
- Leetcode232. Implement Queue using Stacks[栈实现队列]
- ios--uitextfield动态限制输入的字数(解决方式)
- 3.30课·········Marquee标签