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 ...
随机推荐
- linux大全链接
http://man.linuxde.net/
- intellij idea 2018注册码|intellij idea 2018破解文件下载(附破解教程/汉化包)
intellij idea 2018破解文件http://www.3322.cc/soft/37661.html intellij idea 2018注册码是一款针对“intellij idea 20 ...
- android框架---->下沉文字Titanic的使用
Titanic is a simple illusion obtained by applying an animated translation on the TextView TextPaint ...
- JavaIO详解
很全面的内容:http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html
- Excel 2010 如何在Excel的单元格中加入下拉选项
http://jingyan.baidu.com/article/03b2f78c4ba8a05ea237ae95.html 第一步:打开excel文档,选中需加入下拉选项的单元格. 第二步:点击 ...
- Vue如何引入远程JS文件
直接在dom上操作: export default { mounted() { const s = document.createElement('script'); s.type = 'text/j ...
- 关于sencha touch 用phonegap打包后,docked悬停的组件被手机软键盘遮挡的解决方法
这个问题应该算是phonegap的一个bug,在mainifest.xml 里android:windowSoftInputMode设置成了adjustpan,理论上不会出现遮挡悬停组件这种情况, 不 ...
- numeric_limits 模板的相关知识点
说白了,它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个类的成员变量与成员函数中,我们可以了解到int的很 ...
- mysql 一对多,多对多
一对多 一对多与多对一是一个概念,指的是一个实体的某个数据与另外一个实体的多个数据有关联关系. 班级表(一表) 名称 教室 总人数 学科 PHP141115 A814 53 PHP PHP140925 ...
- java网络编程面试题
1.网络编程时的同步.异步.阻塞.非阻塞? 同步:函数调用在没得到结果之前,没有调用结果,不返回任何结果.异步:函数调用在没得到结果之前,没有调用结果,返回状态信息.阻塞:函数调用在没得到结果之前,当 ...