Coderforces 598.B Queries on a String
来源:互联网 发布:淘宝卖衣服货源 编辑:程序博客网 时间:2024/05/16 19:43
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 isabbacaa. If after that we would process the query l2 = 1, r2 = 4, k2 = 2 then we would get the string baabcaa.
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.
Print the resulting string s after processing all m queries.
abacaba23 6 11 4 2
baabcaa
The sample is described in problem statement.
题意:该题是一道模拟字符串平移的题目,不难理解。给出l和r确定区间,每次区间内的每个字母在此区间上右移一个单位,最右边的移至最左边,此操作可重复k次,要求输出最终操作完成后的字符串。
分析:该题可以简单地模拟出答案,但肯定会超时。所以需要找出规律,简化过程。很容易可以观察出:k为区间字符串的长度时,字符串变为原字符串,所以可以让k对区间长度取余后赋值给k,此时k为有效的操作次数。此时可将总体的右移k次抽象成每个字符的右移k个单位,考虑到两种情况,一种是右移k个单位后没有超出r的范围不需要右移至左侧的情况,该种情况直接加k即可,另一种情况是加上k个单位后超出r的范围需要跳至左侧再进行右移操作,此时减去区间范围即可。具体加减数值,笔算举例很容易得出不赘述。
需要注意的是,由于是每个字符的右移运算,需要一个过渡数组来记录原本的值,防止覆盖产生错误。比如原数组a[3]右移三个单位至a[6],若直接赋值给a[6]的话,a[6]的值就已改变,当a[6]右移此时移的是原来a[3]的值,这样应该很容易理解。
见AC代码:
#include<stdio.h>#include<string.h>const int maxn=10005;char a[maxn],b[maxn];int main(){scanf("%s",a);int len=strlen(a);for(int i=len-1; i>=0; i--)a[i+1]=a[i];int m;scanf("%d",&m);while(m--){//如果单纯地模拟m次的话 会超时//所以需要找规律int l,r,num;scanf("%d%d%d",&l,&r,&num);num=num%(r-l+1);//用中间数组记录a数组的初始状态 防止运算过程影响a的值for(int i=l; i<=r; i++)b[i]=a[i];for(int i=l; i<=r; i++){if(i+num>r)b[i+num-(r-l+1)]=a[i];elseb[i+num]=a[i];}for(int i=l; i<=r; i++)a[i]=b[i];}for(int i=1; i<len+1; i++)printf("%c",a[i]);printf("\n");}总结教训,敲代码之前得事先找出规律化简,没有那么多的水题可以让我们用简单逻辑实现。
特记下,以备后日回顾。
- Coderforces 598.B Queries on a String
- CF--B - Queries on a String
- coderforce 598B Queries on a String
- Educational Codeforces Round 1 B. Queries on a String
- Codeforces 598B Queries on a String 【水题】
- Educational Codeforces Round 1 B. Queries on a String
- Educational Codeforces Round 1 B.Queries on a String(模拟)
- Codeforces 598B Queries on a String 【思维】
- [杂题] Codeforces #598B. Queries on a String
- codeforces_598B. Queries on a String
- Educational Codeforces Round 1 B. Queries on a String(字符串简单操作)
- 【Educational Codeforces Round 1B】【字符串平移 水题】Queries on a String 字符串平移水题
- Codeforces 598B Queries on a String(rotate(begin,mid,end))
- codeforces - Queries on a String(模拟+取余)
- 【CodeChef】 Queries on the String
- UVA 12424 Answering Queries on a Tree
- Optimizing MySQL queries on a single table
- Coderforces 507B 易错
- StringBuffer与StringBuilder区别
- Activity官方文档译文简析
- 版本控制之重要操作
- mysql命令(三)--操作数据表中的记录
- JNI WARNING: CallObjectMethod expected return type 'L'
- Coderforces 598.B Queries on a String
- 页面传值
- mysql命令(五)--存储引擎
- MTK6795刷机与启动
- javah执行提示:错误: 编码GBK的不可映射字符
- 修改u-boot的开机logo及显示过程
- NDK编译时报错: Android NDK: NDK Application 'local' targets unknown ABI(s): arm64-v8a x86_64
- mysql命令(六)--子查询和连接
- EasyDarwin开源社区流媒体视频课程:流媒体传输控制协议(RTSP RTP SDP)详解之RTSP