Canada Cup 2016 C. Hidden Word(字符串构造)

来源:互联网 发布:淘宝蜗牛移动充值卡 编辑:程序博客网 时间:2024/05/21 01:48

大体题意:

给你一个字符串,要求构造出一个字符关系来,使得字符串行走顺序必须符合字符关系,其中字符关系是2*13 的字符矩阵, 8个方向都是相邻(当然不可能有8个方向= =)

思路:

这个题长度是固定的  就是27个字符,开始没看见 还以为很长很长,于是想了好久好久都没有思路~ = =!

如果是27个字符 那就随便做着玩了!

题目保证 26个字母保证出现至少一次,那么肯定只有一个字母 出现了2次了!

只有这两个字母是相邻的话,那就是impossible了!

否则一定有解:

举个例子:

%%%%%%G%%%%%%%G%%%%%%

字符串都是这种类型的!

先处理中间的G%%%%G

既然能回到G 那么肯定是这样的:

%%%%G

%%%%

那么先构造出这种形式,然后取第一个G前面的字符串%^&......

既然%是和G相邻那就在G下一个接着构造

%%%%G%^&.......

%%%%

然后是第二个G后面的字符串  &&&&&&&&&

他们也是和G相邻,只能放到第一个G的右下方了!

如下形式:

%%%%G%^&

%%%%&&&&&...

这样构造完就可以了,不要担心填重了,不可能填充,因为字符数量固定!!!!

详细见代码:

#include <bits/stdc++.h>using namespace std;const int maxn = 30;char s[maxn];int p[maxn];int ans[maxn][maxn];int main(){    scanf("%s",s+1);    int len = 27;    memset(p,-1,sizeof p);    int p1,p2;    int val;    for (int i = 1; i <= len; ++i){        int id = s[i] - 'A';        if (~p[id]){            p1 = p[id];            p2 = i;            val = id;            break;        }        else {            p[id] = i;        }    }    if (p1 + 1 == p2)        return 0 * puts("Impossible");    int t = p2-p1-1;    int t2 = t >> 1;    for (int i = 1; i <= t2; ++i){        int id = p1 + t2 - (i-1);        ans[1][i] = s[id] - 'A';    }    ans[1][t2+1] = val;    for (int i = 1; i <= t-t2; ++i){        int id = p1 + t2 + i;        ans[2][i] = s[id] - 'A';    }//    ans[2][t-t2+1] = val;    int pos1 = 1,pos2 = t2+2;    for (int i = p1-1; i > 0; --i){        if (pos2 > 13){            pos2 = 13;            pos1 = 2;        }        ans[pos1][pos2] = s[i] - 'A';        if (pos1 == 1) ++pos2;        else --pos2;    }    pos1 = 2,pos2 = (t & 1) ? t2 + 2 : t2+1;    for (int i = p2 + 1; i <= 27; ++i){        if (pos2 > 13){            pos2 = 13;            pos1 = 1;        }        ans[pos1][pos2] = s[i] - 'A';        if (pos1 == 1) --pos2;        else ++pos2;    }    for (int i = 1; i <= 2; ++i){        for (int j = 1; j <= 13; ++j){            printf("%c",ans[i][j] + 'A');        }        puts("");    }    return 0;}/**ABCDEFGHIJKLMNOPQRSGTUVWXYZM L K J I H G F E D C B AN O P Q R S T U V W X Y ZABCDEFTGHIJKLMNOPQRSGUVWXYZABCDEFTUGHIJKLMNOPQRSGVWXYZGABCDEFHIJKLMNOPQRSTUVWXYZG**/

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 and13 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:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

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 of27 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 strings. If there’s no solution, print "Impossible" (without the quotes).

Input

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

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
YXWVUTGHIJKLMZABCDEFSRQPON
Input
BUVTYZFQSNRIWOXXGJLKACPEMDH
Output
Impossible


0 0
原创粉丝点击