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文件,并修改代码增加了通用性,点击这里查看 今天写小程序,有一个需求就是用户选择时间,然后我这边就要开始倒计时. 因为小程序的限制,所以直接选用时间选择器作为选择定时 ...
随机推荐
- - > 动规讲解基础讲解八——正整数分组
将一堆正整数分为2组,要求2组的和相差最小.例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 整数个数n<=100,所有整数的和<=1 ...
- Srping Boot日志输出(转)
说明:其实经过研究,在最新版本的Spring Boot中默认使用的是logback进行日志输出,其余的都没有引入.但是网上的教程说只要按照下面的文件列表引入对应的配置文件就会进行输出,这个没有实践过, ...
- 17、Java并发性和多线程-避免死锁
以下内容转自http://ifeve.com/deadlock-prevention/: 在有些情况下死锁是可以避免的.本文将展示三种用于避免死锁的技术: 加锁顺序 当多个线程需要相同的一些锁,但是按 ...
- Qt移动应用开发(二):使用动画框架
Qt移动应用开发(二):使用动画框架 上一篇博客介绍了怎样使用Qt的QML来对屏幕分辨率大小进行适应,其实,不同分辨率的适应是一个很棘手的问题,除了分辨率不同外,宽高比(aspect ratio)也不 ...
- Xcode9自带版本控制系统(Git)的使用
情形一:新项目工程 前提是首先你得有个Github账号. 1.创建新工程,选中使用git,创建本地仓库. 2.在偏好设置添加git账号. 3.创建Github远程仓库,xcode会自动帮你在Githu ...
- LeetCode 884. Uncommon Words from Two Sentences (两句话中的不常见单词)
题目标签:HashMap 题目给了我们两个句子,让我们找出不常见单词,只出现过一次的单词就是不常见单词. 把A 和 B 里的word 都存入 map,记录它们出现的次数.之后遍历map,把只出现过一次 ...
- OpenCV2马拉松第25圈——直线拟合与RANSAC算法
计算机视觉讨论群162501053 转载请注明:http://blog.csdn.net/abcd1992719g/article/details/28118095 收入囊中 最小二乘法(least ...
- 5.Swift教程翻译系列——Swift字符串和字符
英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 字符串是一组字符的有序序列,比方"hello,china"或 ...
- Zookeeper01
ZooKeeper数据模型Znode
- MFC基础学习
RECT rect = { }; //获取窗口的内部客户区矩形 GetClientRect(&rect); 模态和费模态对话框! 模态对话框只需要包含对话框头文件,定义对话框类,调用DoMod ...