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[],gra[][];
int vis[]; struct Seg
{
int l,r,len;
} seg[]; int f;
void Front(char s[],int slen)
{
int p=slen/-,p2=;
int p1=p;
while(p1>=)
{
gra[][p1--]=s[p2++];
}
p1++;
while(p1<=p)
{
gra[][p1++]=s[p2++];
}
if(slen%)
gra[][p1]=s[p2];
} void Behind(int slen)
{
int h=seg[].l,t=seg[].r,p=-slen/;
int p1=p;
//cout<<p<<endl;
while(p1<)
{
gra[][p1++]=str[h++];
}
p1--;
while(p1>=p)
{
gra[][p1--]=str[h++];
}
if(slen%)
gra[][p-]=str[h++];
} int main()
{
scanf("%s",str);
for(int i=; i<; i++)
{
if(!vis[str[i]-'A'])
vis[str[i]-'A']=;
else
f=i;
}
int h=,cnt=;
for(int i=; i<; i++)
{
if(str[i]==str[f]&&cnt==)
{
seg[cnt].l=h;
seg[cnt].r=i-;
seg[cnt].len=seg[cnt].r-seg[cnt].l+;
h=i+;
cnt++;
}
else if(str[i]==str[f]&&cnt==)
{
seg[cnt].l=h;
seg[cnt].r=i-;
seg[cnt].len=seg[cnt].r-seg[cnt++].l+;
seg[cnt].l=i+;
seg[cnt].r=;
seg[cnt].len=seg[cnt].r-seg[cnt].l+;
}
}
if(seg[].l>seg[].r)
{
printf("impossible\n");
return ;
}
else
{
char tmp[];
int cntt=;
for(int j=seg[].r; j>=seg[].l; j--)
tmp[cntt++]=str[j];
for(int j=seg[].r; j>=seg[].l; j--)
tmp[cntt++]=str[j];
gra[][cntt/]=str[f];
if(cntt>)
Front(tmp,cntt);
Behind(seg[].len);
}
gra[][]='\0';
gra[][]='\0';
cout<<gra[]<<endl;
cout<<gra[]<<endl;
return ;
}

最新文章

  1. 【原创分享&#183;微信支付】 C# MVC 微信支付教程系列之扫码支付
  2. window使用qt遇到的坑
  3. Unity在编辑器状态下清空控制台信息
  4. Spring 通过FactoryBean配置Bean
  5. 如何通过linux ssh远程linux不用输入密码登入
  6. 联想Y50p预装win8系统改为win7
  7. Android工具包
  8. Xml 学习
  9. ASP.NET问题处理---targetFramwork=‘4.0’错误
  10. winform datetimepacker 开始日期 结束日期 分类: WinForm 2014-07-15 19:14 124人阅读 评论(0) 收藏
  11. Bootstrap验证控件的使用
  12. Plugin execution not covered by lifecycle configuration的解决方案
  13. TypeScript 实现任务队列
  14. Nacos集群环境的搭建与配置
  15. vue.js遍历ajax请求的数据
  16. 个人对于angularjs依赖注入的理解
  17. Python-视图 触发器 事务 存储过程
  18. wap2app(三)-- 添加引导页
  19. php-fpm sock文件权限设置
  20. Chap2:区块链基本技术[《区块链中文词典》维京&amp;甲子]

热门文章

  1. Ubuntu安装deb时错误:“dpkg:错误:另外一个进程已经为 dpkg 状态数据库 加锁”解决
  2. ScrollView双击图片定点放大
  3. strace oracle
  4. Dalvik虚拟机总结
  5. C#之插入排序
  6. 性能测试实战-XYB项目-外网访问
  7. 一个MySQL-JDBC驱动bug引起的血案……
  8. linux下非root用户怎样改动root权限的文件
  9. JPush 初始化失败,直接按照官方文档的格式写的,portal上的包名肯定不会错,mainfest里面直接指定${applicationId}
  10. 页面渐进式消失【JS代码】