Soj10572 SLOM

来源:互联网 发布:童装淘宝怎么拍照技巧 编辑:程序博客网 时间:2024/05/16 08:20


 
10572. SLOM
  
Total:138Accepted:60Rating:
Unrated
    
    
Time Limit: 1sec    Memory Limit:256MB
Description

Little Marin spent all his day generating test data for COCI. He simply couldn't make it work, so he had 

a nervous breakdown and can't even see clearly anymore. Every time he blinks while reading, the letters 

in a word get mixed up so that the letters from the second half of the word (the shorter half, if the 

length is an odd number) "jump in" between the letters from the first half in the following way:

 

 the last letter "jumps in" between the first and the second letter 

• the penultimate letter "jumps in" between the second and the third letter 

• the kth letter from the end "jumps in" between the kth and the (k+1)th letter from the beginning 

 

 

For example, the word "abcdef" would become "afbecd" after blinking. 

If Marin blinks again, the same thing happens. After two blinks, the word "abcdef" becomes "adfcbe". 

 

Marin has decided to write a program to help him determine what's exactly written on the screen. 

Unfortunately, after a day's work, he's simply too tired and he needs your help. You are given X, the 

number of blinks, and the word Marin sees on the screen. Write a program to solve the mystery for 

Marin and determine what was actually the word before he blinked X times. 

 

Input

The first line of input contains a positive integer X (1 ≤ ≤ 1 000 000 000), the number of times 

Marin blinked. 

The second line of input contains the word from the screen, its length being from the interval [3, 1000]. 

The word will consist only from small letters of English alphabet. 

 

Output
The first and only line of output must contain the original word, before Marin blinked X times. 
 
Sample Input
 Copy sample input to clipboard
====Sample 1====4 acefdb====Sample 2====1000 aaaaaa ====Sample 3====11 srama
Sample Output
====Sample 1====abcdef====Sample 2====aaaaaa ====Sample 3====sarma

Problem Source: 2014年每周一赛第四场暨校赛模拟赛I/COCI

    


       题目意思是给一个字符串,描述了一种变换方法,问X次变换后的字符串是什么样子。首先这个变换方法需要自己找出来,对于每种长度的字符串写出一种通用算法构造出置换,在纸上手画一下就可以发现了,然后就是快速找到X次后的序列对应输出就好了,X虽然很大,但是字符串很短最多1000,只要找出循环节对X取模就可以了,然后余数次变换就可以了。首先对求出的变换序列求出循环节长度,在求最小公倍数,置换群基本的思路。

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <iostream>#include <cstdlib>#define INF 0x3f3f3f3fusing namespace std;const int N = 100010;int n;int next[N];bool vis[N];int gcd(int a,int b){    return b?gcd(b,a%b):a;}int lcm(int a,int b){    return a/gcd(a,b)*b;}int id[N],len[N];char ans[N];int dfs(int c,int l,int d){    vis[c] = 1;    if(vis[next[c]])return l;    id[c] = d;    return dfs(next[c],l+1,d);}int main(){ //freopen("in.txt","r",stdin);    int x;    char s[1111];    while(~scanf("%d",&x))    {        scanf("%s",s + 1);        int l = strlen(s + 1);        int hash[1101];        hash[1] = 1;        n = l;        for(int i = 2,j = l,op = 1,c = 0;i <= l;op ^= 1,i++)        {            if(op)hash[i] = j,j--,c++;            else hash[i] = i - c;        }       for(int i = 1;i <= l;i ++)next[i] = hash[i];        int ret = 1;        memset(vis,0,sizeof(vis));        for(int i = 1;i <= l;i ++)            if(!vis[i])            {                id[i] = i;                len[i] = dfs(i,1,i);                ret = lcm(ret,len[i]);            }        // printf("%d\n",ret);        x %= ret;        for(int i = 1;i <= l;i ++)        {            int cur = i,t = x;            while(t)            {                cur = next[cur];                t--;            }            ans[cur] = s[i];        }                 ans[n+1] = '\0';        printf("%s\n",ans + 1);     }    return 0;}  


0 0
原创粉丝点击