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 ...
随机推荐
- simplexml 对xml的增删改操作
simplexml 是php 处理xml 文件的一个方法,另一个是dom 处理,这里只说simplexml .目前php 处理xml 用的比较多,比较成熟的还是dom .但dom 在速度和代码量上还是 ...
- TortoiseSVN使用简介(收藏)
TortoiseSVN使用简介 1.安装及下载client 端 2.什么是SVN(Subversion)? 3.为甚么要用SVN? 4.怎么样在Windows下面建立SVN Repository? 5 ...
- MySQL5.6多实例部署
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://suifu.blog.51cto.com/9167728/1850560 无论是迫 ...
- js原生设计模式——7原型模式之new+call(this)组合应用再探讨实例
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- HDU-1864-最大报销额
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1864 这题开始题意没搞清楚,就做题了,导致浪费了很多的时间,不应该啊, 注意事项:每张发票上,单项物品 ...
- RMAN-06217: not connected to auxiliary database with a net service name
RMAN> duplicate target database to clonedb from active database; Starting Duplicate Db at 28-JAN- ...
- Mac 电脑系统的重装
首先来说一下我为什么会想到重装Mac的系统呢??? 其实呢 很简单的一个理由,在我写上一个项目的时候,在功能code编写完成后,在模拟器上运行是完全没有问题的,但是在真机上就不行,大家也都知道,在X ...
- Java线程:堵塞队列与堵塞栈
一.堵塞队列 Java定义了堵塞队列的接口java.util.concurrent.BlockingQueue,堵塞队列是一个指定长度的队列,当试图向队列中添加元素而队列已满,或者是想从队列移出元素而 ...
- Java线程:锁
一.锁的原理 Java中每个对象都有一个内置锁,当程序运行到非静态的synchronized同步方法上时,自动获得与正在执行的代码类的当前实例(this实例)有关的锁.获得一个对象的锁也称为获取锁.锁 ...
- MyEclipse 中 添加 js自动完成模版
MyEclipse 中 添加 js自动完成模版: window>preference>MyEclipse>Files and Editors>JavaScript>Edi ...