Educational Codeforces Round 1 B. Queries on a String
原题: http://codeforces.com/contest/598/problem/B
题目大意: 给一个string, 给l,r,k三个数, l和r是其中的char的index(以1为底), 求k次右shift后的string
分析: 没有什么算法, 就是注意是以1为底, 然后特殊情况有两种, 一个是k比r-l+1大, 所以取余, 另一个是r==l, 别的都是照常实现就好.
public class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { String s = in.readLine(); char[] chars = s.toCharArray(); int n = in.readInt(); for (int i = 0; i < n; i++) { int l = in.readInt(); int r = in.readInt(); int k = in.readInt(); solve(chars,l-1,r-1,k); } out.print(chars); } private void solve(char[] ary, int l, int r, int k){ char[] tmp = new char[r-l+1]; k %= r-l+1; for(int i = l; i <= r; i++){ tmp[(i-l+k)%(r-l+1)] = ary[i]; } for(int i = l; i <= r; i++){ ary[i] = tmp[i-l]; } } }
Leave A Comment