组队选拔赛01 ---- slom

来源:互联网 发布:戴尔软件开发公司简介 编辑:程序博客网 时间:2024/06/05 00:17

Problem Description

Little Marin spent all his day generating test data for FOJ. 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 k-th letter from the end “jumps in” between the k-th 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

There are multiple test cases. Please process till EOF.
The first line of input contains a positive integer X (1 ≤ X ≤ 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

4
acefdb
1000
aaaaaa
11
srama

Sample Output

abcdef
aaaaaa
sarma

解题思路

题意 一个字符串 经过x次操作 后,会变成哪个串…
首先x的数据规模达到了10^9,直接模拟的话 肯定会超时.
所以 我们可以去找规律— 一个字符串最少要操作次数T后,才能恢复为原来的串
那么 最后只要对 x%T 的次数 进行模拟就行了

参考代码

#include <iostream>#include <string>using namespace std;typedef __int64 ll;int arr[1010],a[1010],t[1010];int main(){    ll x;    string s;    for (int N = 3;N <= 1000;N++){        int cur = 2;        int cnt = 0;        while (true){            if (cur%2 == 0) cur = N-cur/2+1;            else    cur = cur/2+1;            cnt++;            if (cur == 2)   break;        }        arr[N] = cnt;       }    while (cin >> x){        cin >> s;        int len = s.size();        int left = x%arr[len];        for (int i = 0;i <= len;i++)            a[i] = t[i] = i;        for (int i = 0;i < left;i++){            for (int j = 2;j <= len;j++)                if (j%2 == 0) a[len-j/2+1] = t[j];                else a[j/2+1] = t[j];            for (int j = 1;j <= len;j++) t[j] = a[j];        }        for (int i = 0;i < len;i++)            cout << s[a[i+1]-1];        cout << endl;    }    return 0;}
0 0
原创粉丝点击