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 ;
}
最新文章
- ORB
- Mongdb使用客户端
- web项目首页提示404
- hdoj 1045 Fire Net
- Qt Creator error: LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 模拟红外协议C程序——接收模块
- jquery.validate提示错误方法
- nopCommerce 3.9 大波浪系列 之 可退款的支付宝插件(下)
- InnoDB online DDL与快速索引创建
- 【Unity Shaders】Reflecting Your World —— Unity3D中简单的Cubemap反射
- 【数学建模】MATLAB语法
- idea构建spark开发环境,并本地运行wordcount
- 【LeetCode每天一题】Rotate Image(旋转矩阵)
- ORA-01078和LRM-00109问题导致ORACLE启动失败解决方法
- sys系统用户长时间未登录导致密码过期
- 关于部署传统的Dynamic Web项目
- Selenium2(WebDriver)总结(一)---启动浏览器、设置profile&;加载插件
- [转]Python多线程与多线程中join()的用法
- Caffe cpu版本 Linux配置命令及搭建
- windows调试本地启动的tomcat
热门文章
- Angular - - form.FormController、ngModel.NgModelController
- 【bzoj3998】 TJOI2015—弦论
- 在向服务器发送请求时发生传输级错误。 (provider: TCP 提供程序, error: 0 - 远程主机强迫关闭了一个现有的连接。)
- Java jsp 示例
- db2 数据类型
- thinkphp 配置项总结
- C# WInform 界面左导航菜单
- 抽象类 abstract 和 接口 interface 类的区别
- Redis 介绍与安装
- 华为oj---合并数组