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 ...
随机推荐
- 【RF库Collections测试】Dictionary Should Not Contain Value
Name:Dictionary Should Not Contain ValueSource:Collections <test library>Arguments:[ dictionar ...
- upper()
upper() 用于把字符串中的小写字母转换成大写字母 In [1]: str = "Hello World" In [2]: str.upper() Out[2]: 'HELLO ...
- php截取中文字符串时乱码问题
<?php function chinesesubstr($str,$start,$len) { //$str指字符串,$start指字符串的起始位置,$len指字符串长度 $strlen=$s ...
- c/c++左值和右值
C/C++中的变量有左值和右值之分,他们的区别主要如下: (1)左值可以放在赋值号 = 的左右两边,右值只能放在赋值号 = 的右边 (2)在C语言中,有名字的变量即为左值:而函数的运行结果或表达式中间 ...
- 【linux】安装rar,并解压被压缩成多个rar的文件
rar 官网:http://www.rarsoft.com/download.htm 选择 RAR for linux (注意你的系统是32位还是64位) 1 安装命令: $ cd /roo ...
- C++中的inline关键字
from here 1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程 ...
- Docker源码分析(四):Docker Daemon之NewDaemon实现
1. 前言 Docker的生态系统日趋完善,开发者群体也在日趋庞大,这让业界对Docker持续抱有极其乐观的态度.如今,对于广大开发者而言,使用Docker这项技术已然不是门槛,享受Docker带来的 ...
- tomcat的添加及jar包和jQuery的加载
- SSH配置优化和慢的解决方法
author: headsen chen date: 2018-08-18 00:28:37 ssh配置优化 vim /etc/ssh/sshd_config 1,修改root端口 2,不允许ro ...
- linux显示文件列表命令ls,使用ls --help列出所有命令参数
ls命令的相关参数 在提示符下输入ls --help ,屏幕会显示该命令的使用格式及参数信息: 先介绍一下ls命令的主要参数: -a 列出目录下的所有文件,包括以 . 开头的隐含文件. -A 显示除 ...