Codeforces #386 B 746B Decoding

来源:互联网 发布:mac os.ios镜像文件 编辑:程序博客网 时间:2024/06/01 10:29

B. Decoding
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contestinfo. If the word consists of single letter, then according to above definition this letter is the median letter.

Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.

You are given an encoding s of some word, your task is to decode it.

Input

The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word.

The second line contains the string s of length n consisting of lowercase English letters — the encoding.

Output

Print the word that Polycarp encoded.

Examples
input
5logva
output
volga
input
2no
output
no
input
4abba
output
baba
Note

In the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter gwhich was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.

In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.

In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba.


        题意: 这个题繁杂的一些背景就略过了直接说题目的要求是什么。输入的这个长度为n(1<=n<=2000)的字符串s1是由另一个长度为n的字符串s2变化而来,变化规则是:每次都把s2中间(如果长度是偶数就取小的一个。)的字母拿出放到s1的第一个上,由此反复进行直到取完。要求输出s2这个字符串。


       思路:我在比赛中解这个题时,就是根据它的变化规则通过模拟还原了这个字符串s2。既然s1的第一个字母是来源于s2的中间,所以就把它放回s2 的中间,然后就用相同的方法不断的推出s1的每个字母来自于s2的哪个位置就这样进行模拟就行了。结果写了一个比较麻烦的代码,因为这个思路比较简单,所以在比赛中写的时候也没想过优化。


#include<iostream>#include<cstring>using namespace std;int main(){int n;while(cin>>n){char s1[2005];cin>>s1;char s2[2005];//在下面就是模拟的过程奇数长度和偶数长度会有所不同 if(n%2==0){ int tol=1;for(int j=0;j<n;j++){s2[n/2-tol+1]=s1[j];j++;s2[n/2+tol]=s1[j];tol++;}}else{int tol=1;s2[n/2+1]=s1[0];for(int j=1;j<n;j++){s2[n/2+1-tol]=s1[j];j++;s2[n/2+1+tol]=s1[j];tol++;}}//主要上面写挫的地方是这里,这里优化一下应该是可以让代码更简洁的 //cout<<s<<endl;for(int j=1;j<=n;j++){cout<<s2[j];}cout<<endl;//写的是时候为了方便理解这个逻辑所以我下标就从1开始了。}}

         虽然这样的思路也AC了,但是我自己却并不喜欢这个思路。这个题的正确的AC思路应该是下面这个代码。(这个代码是比较简洁的一个。)

#include<iostream>using namespace std;int n,i;string s;int main(){    cin >> n >> s;//虽然一样是模拟,其实只要能多想一下应该马上就能想到这种方式模拟的,     for(i=n-2;i>=0;i-=2) cout << s[i];    for(i=1-n&1;i<n;i+=2) cout << s[i];    return 0;}

         我个人是比较喜欢第二代码的,(虽然不是我写的  ^ - ^)。




0 0
原创粉丝点击