C. Strange Sorting
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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).

Output

After each operation print the current state of string S.

Sample test(s)
input
qwerty
3
4 2
6 3
5 2
output
qertwy
qtewry
qetyrw
Note

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的更多相关文章

  1. codeforces 484C Strange Sorting Codeforces Round #276 (Div. 1) C

    思路:首先 他是对1到k 元素做一次变换,然后对2到k+1个元素做一次变化....依次做完. 如果我们对1到k个元素做完一次变换后,把整个数组循环左移一个.那么第二次还是对1 到 k个元素做和第一次一 ...

  2. agc014F Strange Sorting

    这套题比较简单,以为自己能够独立A掉D和E,或许就能自己A掉F,看来还真是想多了 题意:给一个$n$的全排列,每次操作把$max(a[1],a[2],...,a[i]) = a[i]$的记为$high ...

  3. AGC014-F Strange Sorting

    题意 \(n\)-排列,反复进行:将序列中为前缀最大值的数全部移动到序列末(两种数不改变相对位置),问经过多少次后第一次全部升序排列 做法 定义:用high表示为前缀最大值,low则反之 考虑忽略\( ...

  4. 第二十次codeforces竞技结束 #276 Div 2

    真是状况百出的一次CF啊-- 终于还Unrated了,你让半夜打cf 的我们怎样释怀(中途茫茫多的人都退场了)--虽说打得也不好-- 在这里写一下这一场codeforces的解题报告.A-E的 题目及 ...

  5. 【AtCoder】AGC014

    AGC014 链接 A - Cookie Exchanges 发现两个数之间的差会逐渐缩小,所以只要不是三个数都相同,那么log次左右一定会得到答案 #include <bits/stdc++. ...

  6. Atcoder Grand-014 Writeup

    A - Cookie Exchanges 题面 Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respec ...

  7. AtCoder Grand Contest 014

    AtCoder Grand Contest 014 A - Cookie Exchanges 有三个人,分别有\(A,B,C\)块饼干,每次每个人都会把自己的饼干分成相等的两份然后给其他两个人.当其中 ...

  8. A@GC*014

    A@GC*014 A Cookie Exchanges 卡时跑了1s就输出-1 每次操作会使三个数的极差缩小一半,所以最多\(\log\)次之后就会出现\(A=B=C\)的情况,可以直接判掉 B Un ...

  9. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

随机推荐

  1. GIS-008-ArcGIS JS API 全图

    //待服务加载完成后,设置视野范围到全图范围 layer.on('load', function () { var extent = map.getLayer(map.layerIds[0]).ful ...

  2. python2.0_day19_前端分页功能的实现

    我们前面完成的客户纪录展示,只有4条,如果有上百条就不能在1页中全部展示了,那样是不人性化的.另外一次性取出来,数据量也比较大.假如现在有95条数据,我们想实现一个每页展示20条,那就分为5页.假如我 ...

  3. onTouch

    OnTouchOmOnTouchListenerOnTouchEvent View的事件分发 :    对于事件分发机制,举个简单的例子,在一个Activity中只有一个按钮,如果我们想给这个按钮注册 ...

  4. Apktool源码解析——第一篇

    著名的apktool是android逆向界用的最普遍的一个工具,这个项目的原始地址在这里http://code.google.com/p/android-apktool/,但是你们都懂的在天朝谷歌是无 ...

  5. Linux下多任务间通信和同步-mmap共享内存

    Linux下多任务间通信和同步-mmap共享内存 嵌入式开发交流群280352802,欢迎加入! 1.简介 共享内存可以说是最有用的进程间通信方式.两个不用的进程共享内存的意思是:同一块物理内存被映射 ...

  6. LeetCode——Min Stack

    Description: Design a stack that supports push, pop, top, and retrieving the minimum element in cons ...

  7. ubuntu 信使(iptux) 创建桌面快捷方式

    $ sudo ln -s /usr/bin/iptux ~/桌面/iptux.ln

  8. SRC常见WEB漏洞系列之HTTP-HOST头攻击

    一.背景: web程序需要知道网站的域名比较麻烦,需要使用HTTP的 host头字段: <?php _SERVER["HTTP_HOST"] ?> @app.route ...

  9. 把 Activity 改成 ListActivity继续使用 setContentView

    ListActivity has a default layout that consists of a single, full-screen list in the center of the s ...

  10. Hibernate--快速上手

    一.初识 Hibernate 经典的软件应用体系结构有三层:表示层(提供了与用户交互的接口,实现用户操作界面,展示用户需要的数据).业务逻辑层(完成业务流程,处理表示层提交的数据请求,并将要保存的数据 ...