C Strange Sorting
2 seconds
256 megabytes
standard input
standard output
How many specific orders do you know? Ascending order, descending order, order of ascending length, order of ascending polar angle... Let's have a look at another specific order: d-sorting. This sorting is applied to the strings of length at least d, where d is some positive integer. The characters of the string are sorted in following manner: first come all the 0-th characters of the initial string, then the 1-st ones, then the 2-nd ones and so on, in the end go all the (d - 1)-th characters of the initial string. By the i-th characters we mean all the character whose positions are exactly i modulo d. If two characters stand on the positions with the same remainder of integer division byd, their relative order after the sorting shouldn't be changed. The string is zero-indexed. For example, for string 'qwerty':
Its 1-sorting is the string 'qwerty' (all characters stand on 0 positions),
Its 2-sorting is the string 'qetwry' (characters 'q', 'e' and 't' stand on 0 positions and characters 'w', 'r' and 'y' are on 1 positions),
Its 3-sorting is the string 'qrwtey' (characters 'q' and 'r' stand on 0 positions, characters 'w' and 't' stand on 1 positions and characters 'e' and 'y' stand on 2 positions),
Its 4-sorting is the string 'qtwyer',
Its 5-sorting is the string 'qywert'.
You are given string S of length n and m shuffling operations of this string. Each shuffling operation accepts two integer arguments k andd and transforms string S as follows. For each i from 0 to n - k in the increasing order we apply the operation of d-sorting to the substringS[i..i + k - 1]. Here S[a..b] represents a substring that consists of characters on positions from a to b inclusive.
After each shuffling operation you need to print string S.
The first line of the input contains a non-empty string S of length n, consisting of lowercase and uppercase English letters and digits from 0 to 9.
The second line of the input contains integer m – the number of shuffling operations (1 ≤ m·n ≤ 106).
Following m lines contain the descriptions of the operations consisting of two integers k and d (1 ≤ d ≤ k ≤ n).
After each operation print the current state of string S.
qwerty
3
4 2
6 3
5 2
qertwy
qtewry
qetyrw
Here is detailed explanation of the sample. The first modification is executed with arguments k = 4, d = 2. That means that you need to apply 2-sorting for each substring of length 4 one by one moving from the left to the right. The string will transform in the following manner:
qwerty → qewrty → qerwty → qertwy
Thus, string S equals 'qertwy' at the end of first query.
The second modification is executed with arguments k = 6, d = 3. As a result of this operation the whole string S is replaced by its 3-sorting:
qertwy → qtewry
The third modification is executed with arguments k = 5, d = 2.
qtewry → qertwy → qetyrw
因为我们知道 每次进来一个点 就有一个点 出去 , 这里面可能存在 有些点一直在这个区间中,然后我们可以知道最后进来的那个点出了步数不够,其他情况下一定能够出来,那么我们从这个最后一个点出发模拟出他的最后出去的路径,然后可以发现不再这个路径上的点一定是循环再这个区间内, 因为 每次进来的 一个数必将会出去,那么出去又是在这条路径上,于是他们就一定不能出去了,这样把他们的循环节路径一一记下来,那么就下来的工作就剩下枚举每个点 然后求出他们的最后输出在哪里就可以了,因为对于k-1及以后点点 可以通过当前位置和剩下位置,在路径上找出最后的 位置
在循环节内的点 也可以根据循环节路径计算出最后一次会在那个位置出现
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>
using namespace std;
char str[][];
int k,d,n,len,tim;
int R[],ge;
int RFE[],RE[];
vector<int> F[];
void solve(int st, int loc){
int G=k/d;
int H=k%d;
while(loc!=){
RFE[loc]=;
int rr=loc%d;
int num=rr*G;
if(H!=){
if( H<rr) num+=H;
else num+=rr;
}
num+=loc/d;
loc=num-;
R[ge]=loc;
RE[loc]=ge++;
}
}
void xunhun(int nu, int loc){
F[nu].clear();
int G=k/d;
int H=k%d;
int rw=;
while(RFE[loc]==){
F[nu].push_back(loc);
RFE[loc]=nu;
RE[loc]=rw++;
int rr=loc%d;
int num=rr*G;
if(H!=){
if( H<rr ) num+=H;
else num+=rr;
}
num+=loc/d;
loc=num-;
}
}
void inti(int k){
for(int i=; i<=k; ++i) RFE[i]=;
}
int main()
{
while(scanf("%s",str[])==){
len = strlen(str[]);
scanf("%d",&n);
int now=;
for(int i=; i<n; ++i){
now=-now;
scanf("%d%d",&k,&d);
inti(k);
ge=;
tim=len-k+;
str[now][]=str[-now][];
solve(,k-);
for(int j=k-; j<len; ++j){
int loc;
if( len-j >= ge ) loc=R[ge-]+j-k++ge;
else loc = R[len-j-]+j-k++len-j;
str[now][loc]=str[-now][j];
}
int nu=;
for(int j=; j<k-; ++j)if(RFE[j]==){
xunhun(nu,j); nu++;
}
for(int j=; j<k-; ++j)
if(RFE[j]==){
if(tim>=ge-RE[j]) str[now][ ge-RE[j]- ]=str[-now][j];
else str[now][R[RE[j]+tim]+tim]=str[-now][j]; }else{
int sz=F[RFE[j]].size();
int ddd = F[ RFE[j] ][ (RE[j]+tim%sz)%sz ];
int loc = tim+ddd;
str[now][loc]=str[-now][j];
}
str[now][len]=;
printf("%s\n",str[now]);
}
} return ;
}
C Strange Sorting的更多相关文章
- codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C
思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...
- agc014F Strange Sorting
这套题比较简单,以为自己能够独立A掉D和E,或许就能自己A掉F,看来还真是想多了 题意:给一个$n$的全排列,每次操作把$max(a[1],a[2],...,a[i]) = a[i]$的记为$high ...
- AGC014-F Strange Sorting
题意 \(n\)-排列,反复进行:将序列中为前缀最大值的数全部移动到序列末(两种数不改变相对位置),问经过多少次后第一次全部升序排列 做法 定义:用high表示为前缀最大值,low则反之 考虑忽略\( ...
- 第二十次codeforces竞技结束 #276 Div 2
真是状况百出的一次CF啊-- 终于还Unrated了,你让半夜打cf 的我们怎样释怀(中途茫茫多的人都退场了)--虽说打得也不好-- 在这里写一下这一场codeforces的解题报告.A-E的 题目及 ...
- 【AtCoder】AGC014
AGC014 链接 A - Cookie Exchanges 发现两个数之间的差会逐渐缩小,所以只要不是三个数都相同,那么log次左右一定会得到答案 #include <bits/stdc++. ...
- Atcoder Grand-014 Writeup
A - Cookie Exchanges 题面 Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respec ...
- AtCoder Grand Contest 014
AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...
- A@GC*014
A@GC*014 A Cookie Exchanges 卡时跑了1s就输出-1 每次操作会使三个数的极差缩小一半,所以最多\(\log\)次之后就会出现\(A=B=C\)的情况,可以直接判掉 B Un ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
随机推荐
- 【Android】Android实现自定义带文字和图片的Button
在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最 ...
- jvm的内存模型
转自:https://www.cnblogs.com/dingyingsi/p/3760447.html 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互, ...
- 170530、java 迭代hashmap常用的三种方法
@SuppressWarnings("rawtypes") public class HashMapDemo { //hashMap遍历 public static void ma ...
- HDU_3183_A Magic Lamp
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Oracle 11gR2 RAC监听器原理介绍
一.基本概念 oracle11gR2 RAC开始引入scan概念,一般通过dns服务器或gns服务器解析scan,也可以使用/etc/hosts文件解析scan,只不过oracle官方不建议这样做,h ...
- 缓存策略 半自动化就是mybaitis只支持数据库查出的数据映射到pojo类上,而实体到数据库的映射需要自己编写sql语句实现,相较于hibernate这种完全自动化的框架我更喜欢mybatis
springboot入门(三)-- springboot集成mybatis及mybatis generator工具使用 - FoolFox - CSDN博客 https://blog.csdn.net ...
- scrapy爬虫系列之七--scrapy_redis的使用
功能点:如何发送携带cookie访问登录后的页面,如何发送post请求登录 简单介绍: 安装:pip3 install scrapy_redis 在scrapy的基础上实现了更多的功能:如reques ...
- Python开发【模块】:内置模块
内置模块 1.__import__ # import app目录下的kingadmin.py文件 for app in conf.settings.INSTALLED_APPS: __import__ ...
- const V.S readonly
先上两个例子: ; ; static void Main(string[] args) { Console.WriteLine("A is {0},B is {1}", A, B) ...
- [World Wind学习]22.相机高度和瓦片等级计算
在这里我们看到判断Lod的级别主要有三个条件: * 1.相机视角范围,视角范围越大,所包含的tileSize就越大 * 2.相机与瓦片距离,距离越远,所包含的tileSize也就越大 * 3.相机视锥 ...