codeforces_725C_字符串
2 seconds
256 megabytes
standard input
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).
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 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".
ABCDEFGHIJKLMNOPQRSGTUVWXYZ
YXWVUTGHIJKLM
ZABCDEFSRQPON
BUVTYZFQSNRIWOXXGJLKACPEMDH
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_字符串的更多相关文章
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- 测试一下StringBuffer和StringBuilder及字面常量拼接三种字符串的效率
之前一篇里写过字符串常用类的三种方式<java中的字符串相关知识整理>,只不过这个只是分析并不知道他们之间会有多大的区别,或者所谓的StringBuffer能提升多少拼接效率呢?为此写个简 ...
- java中的字符串相关知识整理
字符串为什么这么重要 写了多年java的开发应该对String不陌生,但是我却越发觉得它陌生.每学一门编程语言就会与字符串这个关键词打不少交道.看来它真的很重要. 字符串就是一系列的字符组合的串,如果 ...
- JavaScript 字符串实用常操纪要
JavaScript 字符串用于存储和处理文本.因此在编写 JS 代码之时她总如影随形,在你处理用户的输入数据的时候,在读取或设置 DOM 对象的属性时,在操作 Cookie 时,在转换各种不同 Da ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Redis的简单动态字符串实现
Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,sds)的抽象类 ...
- ASP.NET加密和解密数据库连接字符串
大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...
- Javascript正则对象方法与字符串正则方法总结
正则对象 var reg = new Regexp('abc','gi') var reg = /abc/ig 正则方法 test方法(测试某个字符串是否匹配) var str = 'abc123'; ...
- 微信小程序中利用时间选择器和js无计算实现定时器(将字符串或秒数转换成倒计时)
转载注明出处 改成了一个单独的js文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...
随机推荐
- CentOS6.5 64位站点压力測试工具webbench
在Apache中有自带的ab命令能够測试服务的压力,而nginx没有自带的命令,必需要採用第三方软件来測试.今天就简介一下webbench对nginx的压力測试,压力測试是对系统管理员和运维人员必须的 ...
- OC-内存管理的一些要点
创建一个BOOK对象,对其属性进行声明 定义. @property 属性声明 定义了对属性的赋值 -(void) dealloc 方法在对象销毁的时候进行调用. #import <Foundat ...
- 表格属就用treegrid
http://maxazan.github.io/jquery-treegrid/ 如果想ajax后台动态添加表格数据然后再形成treegrid,那么可以通过后台给一个对应行索引的数组, 进行动态改变 ...
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
- HDU 2461 线段树扫描线
给出N个矩形,M次询问 每次询问给出R个.问这R个矩形围成的面积 经典扫面线求面积并,对每次询问的R个点离散化一下 #include "stdio.h" #include &quo ...
- Localhost 回环IP 127.0.0.1
LocalHost 127.0.0.1是一个保留地址,用于本地软件測试以及本地进程间通信, 也叫回环IP.回环地址.回送地址(loopback address).不管什么程序,一旦使 用回环IP发送数 ...
- css高级:font-size
body{ font:62.5%/1.6em "Lucida Grande",Verdana,Geneva,Helvetica,Arial,sansserif; }//font-s ...
- 递归,迭代,堆栈三种方式实现单链表反转(C++)
#author by changingivan# 2016.04.12#include <iostream> #include <stack> using namespace ...
- football statistics
https://www.whoscored.com/Players/24328/Show/Edinson-Cavani
- click事件触发也有失灵的时候?
今天做了个手机页面,点击某个按钮->弹出菜单,再点击菜单以外的任意位置->关闭菜单,在其他浏览器里面没有问题,但是在IOS浏览器中并不会关闭. 网上解决这个bug的帖子很多,这篇帖子主要是 ...