Queries on a String

题意:

给你一个字符串s,接着有m次循环移位。
  循环移位的一个操作就是将s的最后一个字符移动到第一个字符的位置,并且将所有其他的字符向右移动一个位置。
  例如,s=‘abacaba‘,查询是L1=3,R1=6,K1=1,那么答案是’abbacaa’(解释:从s第三个位置到第六个位置’acab’,循环1次,把b移到第一位,其他往后移一位,就是’baca’,替换之前的’acab’),之后如果我们再做处理L2=1,R2=4,K2=2,那么答案就变’baabcaa’(解释:首先从第一个位置到第四个位置’abba’,第一次通过移位得到’aabb’,第二次就得到’baab’,替换之前的’abba’)。

输入格式:
  第一行一个字符串s,(1<=s的长度<=10000),s全是小写字母;
  第二行一个整数m,有m个查询;
  接下来有m行,包含三个整数Li,Ri,  Ki(1<=Li<=Ri<=s的长度,Ki<=1000000)。

 输出格式
  输出经过m个查询的s。

 

思路:

如果是真的暴力(模拟每一步的改变) 那真的会超时!

关键还是利用好 k 和区间长度之间的关系

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdlib.h>
 4 #include <cstring>
 5 #include <string>
 6 #include <string.h>
 7 #include <set>
 8 #include <queue>
 9 #include <stdbool.h>
10
11 #define LL long long
12 using namespace std;
13 const int maxn = 1e5 + 10;
14
15 int main()
16 {
17     string s1,s2;
18     cin>>s1;
19     s2=s1;
20     int q;
21     cin>>q;
22     while(q--)
23     {
24         int l,r,k;
25         cin>>l>>r>>k;
26         l--,r--;
27         int len = r-l+1;
28         k%=len;
29         for(int i=0;i<len;i++)
30         {
31             s2[(i+k)%len+l]=s1[l+i]; // 这一步是关键
32         }
33         for(int i=l;i<=r;i++)
34             s1[i]=s2[i];
35     }
36     cout<<s1<<endl;
37 }

原文地址:https://www.cnblogs.com/-Ackerman/p/11369287.html

时间: 2024-08-28 05:10:24

Queries on a String的相关文章

CodeForces 598B Queries on a String

水题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; char s[10000+10]; char tmp[10000+10]; int cnt; int m; int main() { scanf("%s",s); scanf("%d"

Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowercase Latin letters and q queries for this string. Recall that the substring s[l;r] of the string s is the string slsl+1-sr. For example, the substrings

evaluate-division

https://leetcode.com/problems/evaluate-division/ public class Solution { private Map mp; private class Item { public String str; public double prop; public Item(String s, double p) { str = s; prop = p; } } public double[] calcEquation(String[][] equa

使用MySQL-Proxy读写分离时的注意事项

在动手操作前最好先安装好MySQL-Proxy,并配置好MySQL主从服务器.补充:新版MySQL已经内建支持 延迟问题 读写分离不能回避的问题之一就是延迟,可以考虑Google提供的SemiSyncReplicationDesign补丁. 端口问题 MySQL-Proxy缺省使用的是4040端口,如果你想透明的把3306端口的请求转发给4040的话,那么可以: iptables -t nat -I PREROUTING -s ! 127.0.0.1 -p tcp --dport 3306 -j

MySQL 5.6 Replication

MySQL 5.6引入的GTID(Global Transaction IDs)使得其复制功能的配置.监控及管理变得更加易于实现,且更加健壮. 要在MySQL 5.6中使用复制功能,其服务配置段[mysqld]中于少应该定义如下选项: binlog-format:二进制日志的格式,有row.statement和mixed几种类型: 需要注意的是:当设置隔离级别为READ-COMMITED必须设置二进制日志格式为ROW,现在MySQL官方认为STATEMENT这个已经不再适合继续使用:但mixed

pr_debug、dev_dbg等动态调试一

内核版本:Linux-3.14 pr_debug: #if defined(CONFIG_DYNAMIC_DEBUG) /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */ #define pr_debug(fmt, ...) \ dynamic_pr_debug(fmt, ##__VA_ARGS__) #elif defined(DEBUG) #define pr_debug(fmt, ...) \

2017-5-10-Train:Educational Codeforces Round 1

A. Tricky Sum(模拟) In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum. For example, for n = 4 the sum is equal to  - 1 - 2 + 3 - 4 =  - 4, because 1, 2 and 4 are 20, 21

QueryRunner的API

org.apache.commons.dbutils Class QueryRunner java.lang.Object org.apache.commons.dbutils.AbstractQueryRunner org.apache.commons.dbutils.QueryRunner public class QueryRunner extends AbstractQueryRunner Executes SQL queries with pluggable strategies fo

在 Java 中高效使用锁的技巧--转载

竞争锁是造成多线程应用程序性能瓶颈的主要原因 区分竞争锁和非竞争锁对性能的影响非常重要.如果一个锁自始至终只被一个线程使用,那么 JVM 有能力优化它带来的绝大部分损耗.如果一个锁被多个线程使用过,但是在任意时刻,都只有一个线程尝试获取锁,那么它的开销要大一些.我们将以上两种锁称为非竞争锁.而对性能影响最严重的情况出现在多个线程同时尝试获取锁时.这种情况是 JVM 无法优化的,而且通常会发生从用户态到内核态的切换.现代 JVM 已对非竞争锁做了很多优化,使它几乎不会对性能造成影响.常见的优化有以