zoukankan      html  css  js  c++  java
  • codeforces_725C_字符串

    C. Hidden Word
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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 

    感觉不难,但是做了很久。。。
    题意:给一个27位字符串,英文中26个字符都必须出现一次,构造一个两行的矩阵,让原字符串中相邻的字符,在矩阵中也相邻,对角线也算相邻。
    思路:将原字符串中的字符分为两种,一种是在重复字符左边的,一种是在其右边的。
    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    char str[30],gra[2][14];
    int vis[30];
    
    struct Seg
    {
        int l,r,len;
    } seg[4];
    
    int f;
    void Front(char s[],int slen)
    {
        int p=slen/2-1,p2=0;
        int p1=p;
        while(p1>=0)
        {
            gra[0][p1--]=s[p2++];
        }
        p1++;
        while(p1<=p)
        {
            gra[1][p1++]=s[p2++];
        }
        if(slen%2)
            gra[1][p1]=s[p2];
    }
    
    void Behind(int slen)
    {
            int h=seg[1].l,t=seg[1].r,p=13-slen/2;
            int p1=p;
            //cout<<p<<endl;
            while(p1<13)
            {
                    gra[0][p1++]=str[h++];
            }
            p1--;
            while(p1>=p)
            {
                    gra[1][p1--]=str[h++];
            }
            if(slen%2)
                    gra[1][p-1]=str[h++];
    }
    
    int main()
    {
        scanf("%s",str);
        for(int i=0; i<27; i++)
        {
            if(!vis[str[i]-'A'])
                vis[str[i]-'A']=1;
            else
                f=i;
        }
        int h=0,cnt=0;
        for(int i=0; i<27; i++)
        {
            if(str[i]==str[f]&&cnt==0)
            {
                seg[cnt].l=h;
                seg[cnt].r=i-1;
                seg[cnt].len=seg[cnt].r-seg[cnt].l+1;
                h=i+1;
                cnt++;
            }
            else if(str[i]==str[f]&&cnt==1)
            {
                seg[cnt].l=h;
                seg[cnt].r=i-1;
                seg[cnt].len=seg[cnt].r-seg[cnt++].l+1;
                seg[cnt].l=i+1;
                seg[cnt].r=26;
                seg[cnt].len=seg[cnt].r-seg[cnt].l+1;
            }
        }
        if(seg[1].l>seg[1].r)
        {
            printf("impossible
    ");
            return 0;
        }
        else
        {
            char tmp[30];
            int cntt=0;
            for(int j=seg[0].r; j>=seg[0].l; j--)
                tmp[cntt++]=str[j];
            for(int j=seg[2].r; j>=seg[2].l; j--)
                tmp[cntt++]=str[j];
                gra[0][cntt/2]=str[f];
            if(cntt>0)
                    Front(tmp,cntt);
            Behind(seg[1].len);
        }
        gra[0][13]='';
        gra[1][13]='';
        cout<<gra[0]<<endl;
        cout<<gra[1]<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    大数据Hadoop-2
    大数据Hadoop-1
    Consistent Hashing
    分支的创建、删除、切换、合并以及冲突解决
    windows WEB 高可用/可伸缩
    Oracle行转列、列转行的Sql语句总结
    从零到百亿互联网金融架构发展史---架构变迁
    WebJars
    springcloud(五):熔断监控Hystrix Dashboard和Turbine
    SpringBoot编写自定义的starter 专题
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6102762.html
Copyright © 2011-2022 走看看