Canada Cup 2016 C. Hidden Word
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 思路:先将初始字符串t删去重复的字母,然后按初始的顺序枚举可能的字符组合(26种),将其分成两行,判断是否符合条件即可。
总结:这是一道思维题,简单搜索的部分,也是对编码功底的锻炼。写了(改了)好久,好久。。
代码:
#include "cstdio"
#include "algorithm"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "stdlib.h"
#include "set"
typedef long long ll;
using namespace std;
const int N=1e4+;
const int mod=1e9+;
#define db double
//int a[N];
//set<int> q;
//map<int ,int > u;
//ll dp[N];
char t[];
char f[];
int m=,pd;
int a[],b[];
char s[][],g[];
int d[][]={{,},{,-},{,},{-,},{-,-},{-,},{,},{,-}};//刚开始没带等号
void dfs(int x,int y,int m){
if(m==) pd=;//m写成26了,最后的错误
a[s[x][y]-'A']--;
for(int i=;i<;i++){
int nx=x+d[i][],ny=y+d[i][];
if(nx>=&&nx<&&ny>=&&ny<&&s[nx][ny]==g[m]&&a[s[nx][ny]-'A']>) {
dfs(nx, ny, m + );
}
}
}
int main()
{
scanf("%s",t);
strcpy(g,t);
char e,v;
for(int i=;i<;i++){
if(t[i]==t[i+]) {puts("Impossible");return ;}
v=t[i];
a[v-'A']++;//'A'写成'a'一直没查出来
if(a[v-'A']==) {
e=v;
for(int j=i+;j<;j++)
a[t[j]-'A']++;
for(int j=i+;j<;){
t[i++]=t[j++];
}
}
}
for(int i=;i<;i++){
b[i]=a[i];
t[i+]=t[i];
}
for(int i=;i<;i++){
int p=;
for(int j=i;j<+i;){
f[p++]=t[j++];
}
int k=;
for(int l=;l<;l++) s[][l]=f[l];//开始数组开13,开小了
for(int l=;l>=;l--) s[][l]=f[k++];
pd=;
for(int ii=;ii<;ii++){
for(int j=;j<;j++){
if(s[ii][j]==t[]){
dfs(ii,j,);
if(pd) {
for(int kk=;kk<;kk++)
puts(s[kk]);
return ;
}
}
}
}
memcpy(a,b, sizeof(a));
}
return ;
}
Canada Cup 2016 C. Hidden Word的更多相关文章
- Canada Cup 2016 C. Hidden Word 构造模拟题
http://codeforces.com/contest/725/problem/C Each English letter occurs at least once in s. 注意到题目有这样一 ...
- 【Codeforces Round 725】Canada Cup 2016
模拟Canada Cup 2016,ABC三题,Rank1376 第三题卡住了 Codeforces 725 C 求出两个相同字符的位置,记为x和y. 然后考虑把相同的那个字符放在第一行的什么地方, ...
- Codeforces Canada Cup 2016
A. Jumping Ball time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Canada Cup 2016 D. Contest Balloons
最近好弱做什么题目都是做一晚上 这是合肥站炼铜后遗症? 这题就是贪心 我已开始还写了1小时---三分-----. #include<bits/stdc++.h> using namespa ...
- Canada Cup 2016 D. Contest Balloons 好题。优先队列 + 简单贪心
http://codeforces.com/contest/725/problem/D 这题一看就是贪心的了,w - t最小的那个,肯定是优先打死. 但是一直都不会写,为什么呢,因为这个太像二分答案了 ...
- CodeForces Canada Cup 2016【A,B,C,D】
CodeForces 725A: 思路就是如果"最左"不是'>'这个了,那么这个右边的一定不可能到达左边了: 同理最右: CodeForces 725B: 有两个空姐,一个从 ...
- Hidden Word
Hidden Word time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- [Google Codejam] Round 1A 2016 - The Last Word
[Problem Description] Problem On the game show The Last Word, the host begins a round by showing the ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) A. Bear and Reverse Radewoosh 水题
A. Bear and Reverse Radewoosh 题目连接: http://www.codeforces.com/contest/658/problem/A Description Lima ...
随机推荐
- 插入排序法-java案例详解
/** * 功能:插入排序法 * 基本思想:把n个待排序的元素看成一个有序和无序表,开始时有序表中只包含一个元素, * 无序表中包含有n-1个元素,排序过程中每次从无序表中取出第一个元素,把它的排序码 ...
- 网站安全配置(Nginx)防止网站被攻击(包括使用了CDN加速之后的配置方法)
原文链接:http://www.bzfshop.net/article/176.html 网站被攻击是一个永恒不变的话题,网站攻击的方式也是一个永恒不变的老套路.找几百个电脑(肉鸡),控制这些电脑同时 ...
- --@angularJS--路由插件UI-Router
UI-Router是angular路由插件,上一篇我们讲到了angularJS自带路由,可惜在路径嵌套上表现的有所欠缺,而angular-UI-Router插件正好弥补了这一点. [示例]: □.UI ...
- AngularJS中$http服务的简单用法
我们可以使用内置的$http服务直接同外部进行通信.$http服务只是简单的封装了浏览器原生的XMLHttpRequest对象. 1.链式调用 $http服务是只能接受一个参数的函数,这个参数是一个对 ...
- jackson - 生成jason工具-简单示例
主页: http://jackson.codehaus.org/ https://github.com/FasterXML/jackson 当前jackson分为三部分,需要分别下载: jackson ...
- CSS页面渲染优化属性will-change
前面的话 当我们通过某些行为(点击.移动或滚动)触发页面进行大面积绘制的时候,浏览器往往是没有准备的,只能被动使用CPU去计算与重绘,由于没有事先准备,应付渲染够呛,于是掉帧卡顿.而CSS属性wi ...
- Crontab could not create directory .ssh
最近在利用 crontab 构建自动备份时,遇到了一个问题.我的脚本中包含了用于服务器用户切换使用的 ssh 命令.当我登录到服务器上时,脚本执行正常:当我没有登录到服务器上时,脚本执行失败,错误提示 ...
- JS与浏览器的几个兼容性问题
第一个:有的浏览器不支持getElementsByClassName(),所以需要写一个function()来得到需要标签的class,然后进行class的增加.删除等操作. 第二个:在需要得到特定标 ...
- 在C++中反射调用.NET(一)
为什么要在C++中调用.NET 一般情况下,我们常常会在.NET程序中调用C/C++的程序,使用P/Invoke方式进行调用,在编写代码代码的时候,首先要导入DLL文件,然后在根据C/C++的头文件编 ...
- C++ 构造函数和析构函数的调用顺序、虚析构函数的作用
构造函数和析构函数的调用顺序 构造函数的调用顺序: 当建立一个对象时,首先调用基类的构造函数,然后调用下一个派生类的构造函数,依次类推,直至到达最底层的目标派生类的构造函数为止. 析构函数的调用书序: ...