zoukankan      html  css  js  c++  java
  • 【36.11%】【codeforces 725C】Hidden Word

    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don’t have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:

    ABCDEFGHIJKLM
    NOPQRSTUVWXYZ
    We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter ‘A’ is adjacent only to the tiles with letters ‘B’, ‘N’, and ‘O’. A tile is not adjacent to itself.

    A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, “ABC” is a path, and so is “KXWIHIJK”. “MAB” is not a path because ‘M’ is not adjacent to ‘A’. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).

    You’re given a string s which consists of 27 upper-case English letters. Each English letter occurs at least once in s. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string s. If there’s no solution, print “Impossible” (without the quotes).

    Input
    The only line of the input contains the string s, consisting of 27 upper-case English letters. Each English letter occurs at least once in s.

    Output
    Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print “Impossible”.

    Examples
    input
    ABCDEFGHIJKLMNOPQRSGTUVWXYZ
    output
    YXWVUTGHIJKLM
    ZABCDEFSRQPON
    input
    BUVTYZFQSNRIWOXXGJLKACPEMDH
    output
    Impossible

    【题解】

    构造题;
    这道题的输入会保证每个字母最多出现两次.
    因为输入说每个字母至少出现一次,一共有26个字母,输入27个字符。则只有一个重复的,其他都只出现一次;
    这里写图片描述
    如上图所示的方法构造;
    上面两个正方形是输入里面相同的字符所在的位置;
    之后再把两边填充一下就好;
    输入如果有两个连续的相同字符则不能构造出上图所述情况;

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <string>
    #define LL long long
    
    using namespace std;
    
    int s3[3][30+300] = { 0 };
    string s1;
    int cnt[30+300];
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        int dis,pos,yuan;
        cin >> s1;
        int len = s1.size();
        string s2 = " ";
        s2 += s1;
        s1 = s2;
        for (int i = 1; i <= len; i++)
        {
            int t = s1[i] - 'A' + 1;
            if (!cnt[t])
                cnt[t] = i;
            else
            {
                if (cnt[t] == i - 1)//连续的则不可能
                {
                    puts("Impossible");
                    return 0;
                }
                dis = i - cnt[t] + 1;//记录前一个和当前所在的位置的差
                pos = cnt[t];//从前一个位置的开始构造
                yuan = i;
                break;
            }
        }
        int st = (13 - dis/2)+1,now=2;//st是第二行的开始位置
        s3[2][st] = s1[pos]-'A'+1;//now=2表示在搞第二行
        st++, pos++;
        int fx = 1;//这是第二行从左往右
        while (s1[pos] != s1[yuan])
        {
            if (st > 13)//开始搞第二行
            {
                st = 13;
                fx = -1;//第二行是从右往左
                now = 1;//now是当前的行数
            }
            s3[now][st] = s1[pos]-'A'+1;//赋值
            pos++;//搞下一个
            st +=fx;
        }
        if (s1[1] != s1[27])//最后一个字符和第一个相同则不需要填充两边了
        {
            pos++;
            if (pos > 27)//要按照第一行的最右到最左然后从第二行的最左到最右,这样就算又变成第一个,也能继续处理,且答案也是正确的;
                pos = 1;
            s3[now][st] = s1[pos] - 'A' + 1;
            pos++;
            if (pos > 27)
                pos = 1;
            st--;
            while (s1[pos] != s1[yuan])
            {
                if (st < 1)//小于1表示要到第二行了;
                {
                    st = 1;
                    fx = 1;//方向变成从左到右了;
                    now = 2;//第二行继续搞;
                }
                int t = s1[pos] - 'A' + 1;
                s3[now][st] = t;
                pos++;
                if (pos > 27)
                    pos = 1;
                st += fx;
            }
        }
        for (int i = 1; i <= 13; i++)
            putchar(char(s3[1][i] + 'A' - 1));
        puts("");
        for (int i = 1;i <= 13;i++)
            putchar(char(s3[2][i] + 'A' - 1));
        puts("");
        return 0;
    }
  • 相关阅读:
    老大叔开博感想
    模板
    Codeforces Round #685 (Div. 2) 题解
    CF830E Perpetual Motion Machine 题解
    THUWC2020游记
    数论
    后缀数组学习笔记
    Codeforces Round #466 (Div. 2)
    博客停写,搬家到www.54kaikai.com
    lda 主题模型--TOPIC MODEL--Gibbslda++结果分析
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632122.html
Copyright © 2011-2022 走看看