codeforces 725C Hidden Word(构造)

来源:互联网 发布:氮素网络是什么意思 编辑:程序博客网 时间:2024/06/03 19:38

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:

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 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
YXWVUTGHIJKLMZABCDEFSRQPON
input
BUVTYZFQSNRIWOXXGJLKACPEMDH
output
Impossible


题意:每个字母能够走到上下左右,左上,右上,左下,右下八个方向。 在图中字母的走向便是路径。 现在给出一个长度为27(26个大写字母每一个最少出现一次)的路径,请输出一个能够走出这个路径的 2*13 的字母图。不存在输出 Impossible


题解: 简单的画一画就可以知道出现了两次的那个字母如果挨在一起就是无解的。(例如: ABCDDEFGHIJKLMNOPQRSTUVWXYZ)  重复出现的字母间距离大于1的串都有解,那么我们找到出现两次的字母第一次出现的位置pos,以及两个字母之间的距离dis,将这个位置放在第一行的 13-(dis/2) 的位置。 然后按照路径串将其余的字母按顺序填入图中就行了,注意第二次出现的那个字母不填。


代码如下:


#include<iostream>#include<cstring>#include<cmath>#include<algorithm>using namespace std;char str[30];int mark[27];char ans[2][13];int main(){int i,j,dis,temp,step;while(cin>>str){memset(mark,-1,sizeof(mark));int flag=0;for(i=0;i<27;++i){if(mark[str[i]-'A']==-1)mark[str[i]-'A']=i;else{temp=mark[str[i]-'A'];dis=i-mark[str[i]-'A'];if(dis==1)flag=1;break;}}if(flag)cout<<"Impossible"<<endl;else{if(dis&1)dis++;i=dis/2;i=13-i;ans[0][i++]=str[temp];step=temp;while(i<13)//先填到第一行的末尾 {ans[0][i]=str[++step];i++;}i=12;while(i>=0&&step<26)//从第二行的末尾往前填 {if(str[++step]!=str[temp]){ans[1][i]=str[step];i--;}}flag=1;if(step==26)//填到路径串的最后一位时,需要从第一位填到重复字母第一次出现的位置 {step=0;flag=0;}while(i>=0){ans[1][i]=str[step];i--;step++;}i=0;while(step<26&&flag){//从第一行的前面开始填 ans[0][i]=str[++step];i++;}if(step==26)step=0;while(step<temp){ans[0][i]=str[step];step++;i++;}for(i=0;i<2;++i){for(j=0;j<13;++j)printf("%c",ans[i][j]);printf("\n");}}}return 0;}//ABCDEFGHIJKLMNOPQRSTUVWXYZA





0 0