zoukankan      html  css  js  c++  java
  • Codeforces Round #386 (Div. 2) B. Decoding

    B. 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.

    题意:一个人将原本正常顺序的单词拆分后重新排列。拆分规则是,每次拿出单词中间的字母,如果单词长度是偶数,取中间两个中左边的字母,直到取完单词中所有字母,按取出的顺序重新排列字母,得到一个新的单词,如volga,依次取出l,o,g,v,a后得到新单词logva,你的任务是,输入改变后的的单词,输出改变前的单词。
    题解:逆向模拟一遍即可。每次取出的第一个字母便是原来单词中最中间的左边字母。

    #include<stdio.h>
    int main()
    {
        char a[2008],b[2008];
        int i,j,n;
        while(scanf("%d",&n)!=EOF)
        {
            scanf("%s",a);
            if(n%2!=0)
            {
                b[n/2]=a[0];
                for(i=1,j=1; i<n; i++,j++)
                {
                    b[n/2-j]=a[i++];
                    b[n/2+j]=a[i];
                }
                b[n]='';
                printf("%s
    ",b);
            }
            else
            {
                n--;
                b[n/2]=a[0];
                for(i=1,j=1; i<=n-1; i++,j++)
                {
                    b[n/2+j]=a[i++];
                    b[n/2-j]=a[i];
                }
                b[n/2+j]=a[i];
                b[n+1]='';
                printf("%s
    ",b);
            }
        }
    }
    
  • 相关阅读:
    P1903 [国家集训队]数颜色 / 维护队列 莫对算法
    P1016 旅行家的预算 模拟 贪心
    P3948 数据结构 差分数组
    乘法逆元 模板
    二分法 最大化平均值
    HDU5213 Lucky 莫队算法 容斥定理
    P1083 借教室 差分数组
    发布订阅、redis的配置文件、redis的主从、redis的持久化、
    nosql、redis、性能测试、命令相关、redis的数据类型string、list、hash、set、zset、
    nginx的日志、禁止访问、反向代理、权重、nginx location匹配规则、location分离、WSGI、
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11794329.html
Copyright © 2011-2022 走看看