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 ;
}

codeforces_725C_字符串的更多相关文章

  1. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  2. 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率

    之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...

  3. java中的字符串相关知识整理

    字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...

  4. JavaScript 字符串实用常操纪要

    JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...

  5. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  6. Redis的简单动态字符串实现

    Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,sds)的抽象类 ...

  7. ASP.NET加密和解密数据库连接字符串

    大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...

  8. Javascript正则对象方法与字符串正则方法总结

    正则对象 var reg = new Regexp('abc','gi') var reg = /abc/ig 正则方法 test方法(测试某个字符串是否匹配) var str = 'abc123'; ...

  9. 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)

    转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...

随机推荐

  1. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  2. Ubuntu 16.04在启动和关机时不显示启动和关机画面且显示详细的命令信息,没有进度条和Logo,或者只有紫色界面,或者没有开机画面等问题解决

    主要有以下解决方法: 1.如果之前配置过Grub来显示详细的命令信息的,那么改回去就行了,参考:http://www.cnblogs.com/EasonJim/p/7129873.html,通过这种方 ...

  3. Filter过滤器机制

    tomcat内部过滤器采用了责任链的设计模式, Tomcat的过滤器主要由Filter.FilterChain组成,FilterChain包含一个Filter数组.当Wrapper执行FilterCh ...

  4. Mysql Innodb存储引擎 insert 死锁分析

    http://chenzhenianqing.cn/articles/1308.html

  5. Windows下Redis的安装与部署

    1.下载地址:https://github.com/MSOpenTech/redis/releases 2.下载zip的包,下载后放到需要安装的目录进行解压操作,列如:F:\Redis\Redis-x ...

  6. 安卓下载文件怎样更新UI进度

    曾经写过几篇关于下载的文章.总的来说是下面几点: 1.维护一个下载进程的Hashmap,key:使用Md5进行处理后的文件下载地址,value为下载的Task. 以防止下载反复.并将信息保存至数据库. ...

  7. LeetCode 463. Island Perimeter (岛的周长)

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  8. S6十大特性

    http://www.alloyteam.com/2016/03/es6-front-end-developers-will-have-to-know-the-top-ten-properties/

  9. 4.4系统,拍照-裁剪,resultCode返回0

    问题描述: take photo -> 拍照 -> 确定 -> 截图 -> 保存,此时返回给onActivityResult的resultCode是0,截图无效.我查看图片储存 ...

  10. SQL 字符串处理函数大全

    select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...