zoukankan      html  css  js  c++  java
  • 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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: contest, info. 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
    5
    logva
    output
    volga
    input
    2
    no
    output
    no
    input
    4
    abba
    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 g which 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.

    【题目链接】:http://codeforces.com/contest/746/problem/B

    【题解】

    一开始以为是顺着来;
    结果发现是要你倒推出原来的字符串是啥。
    模拟一下就好;
    奇数的话是/2 + 1
    偶数位置就是/2

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n;
    string s;
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        cin >> n;
        cin >> s;
        reverse(s.begin(),s.end());
        int len = s.size();
        s = ' '+s;
        string ans = " ";
        int now = 0;
        rep1(i,1,len)
        {
            if (now == 0)
                ans+=s[i],now++;
            else
            {
                int tnow = now+1,po;
                if (tnow&1)
                {
                    po = (tnow/2) + 1;
                    string temp ="";
                    temp += s[i];
                    ans.insert(po,temp);
                }
                else
                {
                    po= (tnow/2);
                    string temp ="";
                    temp += s[i];
                    ans.insert(po,temp);
                }
                now++;
            }
        }
        ans.erase(0,1);
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    【转载】两军问题与Paxos算法 & 动画讲解Paxos算法
    hdu4611 Balls Rearrangement
    [置顶] github简单使用
    set与map容器
    [置顶] C++为什么是C++而不是++C
    HDU 4616 Game (搜索)、(树形dp)
    Just learn how to use the JNI
    HDU 4611 Balls Rearrangement (数学-思维逻辑题)
    冒泡排序改进
    Python基础
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626800.html
Copyright © 2011-2022 走看看