原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780

1780. Gray Code

Time limit: 0.5 second
Memory limit: 64 MB
Denis, Vanya and Fedya gathered at their first team training. Fedya told them that he knew the algorithm for constructing aGray code.
  1. Create a 2-bit list: {0, 1}.
  2. Reflect this list and concatenate it with the original list: {0, 1, 1, 0}.
  3. Prefix old entries with 0, and prefix new entries with 1: {00, 01, 11, 10}.
  4. Repeat steps 2 and 3 until the length of all elements is equal to n.

The number n is a length of a Gray code. For example, the code of length 3 is: {000, 001, 011, 010, 110, 111, 101, 100}.

Denis ran the Fedya's algorithm and obtained a binary number x at position k (positions are numbered starting from zero). Vanya wrote down the numbers k and x in binary system. This story happened many years ago and now you hold the paper sheet with these numbers in your hands. Unfortunately, some digits are unreadable now. Could you determine the values of these digits using the readable digits?

Input

The first line contains a number k written in the binary system. Unreadable digits are denoted with symbol “?”. The second line contains a number x in the same format. The lengths of these numbers are equal and don't exceed 105. The numbers may contain leading zeroes.

Output

If there is a unique way to restore the numbers k and x, output them, replacing the symbols “?” with “0” or “1”. If there are multiple ways to restore them, output “Ambiguity”. If Denis or Vanya certainly made a mistake in these numbers, output “Impossible”.

Samples

input output
0?1
0?0
011
010
?00
??0
Ambiguity
100
100
Impossible

题意

给你个格雷码和二进制,其中一些位置不确定,问你能不能相互转换,是否有多解。

题解

就乱搞,顺着推一次,然后倒着推一次,最后如若还有问号则多解,如果推的过程中有矛盾,则无解。至于为啥要推两次,大家脑补脑补就知道了。

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define MAX_N 100005
using namespace std; char k[MAX_N],x[MAX_N]; bool deal(char &a,char &b,char &c) {
if (a == '?') {
if (b == '') {
if (c != '?')a = c;
return true;
}
else if (b == '') {
if (c != '?')
a = '' + '' - c;
return true;
}
}
else if (a == '') {
if (b != c && b != '?' && c != '?')return false;
if (b == '?')b = c;
if (c == '?')c = b;
return true;
}
else {
if (b == c && b != '?')return false;
if (b == '?' && c != '?')b = '' + '' - c;
if (c == '?' && b != '?')c = '' + '' - b;
return true;
}
} int main() {
scanf("%s%s", k, x);
bool now = ;
int n = strlen(k);
for (int i = ; i < n; i++) {
char tmp = '';
bool flag = true;
if (i == )flag = deal(tmp, k[i], x[i]);
else flag = deal(k[i - ], k[i], x[i]);
if (!flag) {
cout << "Impossible" << endl;
return ;
}
}
for (int i = n - ; i >= ; i--) {
char tmp = '';
bool flag = true;
if (i == )flag = deal(tmp, k[i], x[i]);
else flag = deal(k[i - ], k[i], x[i]);
if (!flag) {
cout << "Impossible" << endl;
return ;
}
}
bool flag = true;
for (int i = ; i < n; i++)
if (x[i] == '?') {
flag = false;
break;
}
for (int i = ; i < n; i++)
if (k[i] == '?') {
flag = false;
break;
}
if (flag)
printf("%s\n%s", k, x);
else printf("Ambiguity\n");
return ;
}

Ural 1780 Gray Code 乱搞暴力的更多相关文章

  1. [NOI.AC省选模拟赛3.30] Mas的童年 [二进制乱搞]

    题面 传送门 思路 这题其实蛮好想的......就是我考试的时候zz了,一直没有想到标记过的可以不再标记,总复杂度是$O(n)$ 首先我们求个前缀和,那么$ans_i=max(pre[j]+pre[i ...

  2. URAL 1780 G - Gray Code 找规律

    G - Gray CodeTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

  3. VIJOS1476 旅行规划(树形Dp + DFS暴力乱搞)

    题意: 给出一个树,树上每一条边的边权为 1,求树上所有最长链的点集并. 细节: 可能存在多条最长链!最长链!最长链!重要的事情说三遍 分析: 方法round 1:暴力乱搞Q A Q,边权为正-> ...

  4. URAL 1827 Indigenous Wars(排序、乱搞)

    题意:给一个长度为n数组{a[i]}.有m个操作Ti,Si,Li表示找以Ti值结束,以Si值开始,长度为Li的连续子串.找到后,将区间的答案值设为1.一开始答案值全部为0.最后输出n个答案值. 好久没 ...

  5. URAL - 1920 Titan Ruins: the Infinite Power of Magic(乱搞)

    搞死人的题目,,, 就是在n*n的方格中找路径长度为L的回路. 开始的思路值适合n为偶数的情况,而忽视了奇数的case,所以wa了一次. 然后找奇数case的策略,代码从70多行变成了100多,然后改 ...

  6. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

  7. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)

    A. Carrot Cakes time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. CodeForces - 1228D (暴力+思维+乱搞)

    题意 https://vjudge.net/problem/CodeForces-1228D 有一个n个顶点m条边的无向图,在一对顶点中最多有一条边. 设v1,v2是两个不相交的非空子集,当满足以下条 ...

  9. [CSP-S模拟测试]:Cicada拿衣服(暴力+乱搞)

    题目传送门(内部题94) 输入格式 第一行两个整数$n,k$,代表衣服的数量和阈值. 接下来一行$n$个数,第$i$个数$a_i$表示每件衣服的愉悦值. 输出格式 输出一行$n$个数,第$i$个数为$ ...

随机推荐

  1. graph-basic

    打算使用STL中的vector,通过邻接链表的方式存储图.这里贴基本定义,以及depth-first-search和breadth-first-search的实现代码. 其他图的算法实现,就贴在各自的 ...

  2. (转)rvm安装与常用命令

    rvm是一个命令行工具,可以提供一个便捷的多版本ruby环境的管理和切换. https://rvm.io/ 如果你打算学习ruby/rails, rvm是必不可少的工具之一. 这里所有的命令都是再用户 ...

  3. C++中重载、覆盖和隐藏的区别,以及适用场景

    一.重载.覆盖和隐藏的区别 二.适用场景 1.重载: 适用于不同的数据类型都需要使用到的功能函数.以数据相加的函数为例,可以在同一个文件内提供以下的重载函数以支持同样的功能: int add(int, ...

  4. 树状数组:CDOJ1583-曜酱的心意(树状数组心得)

    曜酱的心意 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 131072/131072KB (Java/Others) Description ...

  5. Mysql主键一致时,可以进行在元数据上的操作

    insert into daliy_hit_counter(day,slot,cnt) values(12,12,1) on duplicate key update cnt = cnt +1 dal ...

  6. Hadoop4.2HDFS测试报告之五

    第二组:文件存储读过程记录 NameNode:1 DataNode:1 本地存储 scp romotepath localpath 500 2 1 23.05 NameNode:1 DataNode: ...

  7. BZOJ 4479: [Jsoi2013]吃货jyy

    一句话题意:求必须包含某K条边的回路(回到1),使得总权值最小 转化为权值最小的联通的偶点 令F[i]表示联通状态为i的最小权值,(3^n状压)表示不在联通块内/奇点/偶点,连边时先不考虑必选的边的度 ...

  8. 03008_ServletContext

    1.什么是ServletContext? (1)ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象    内部封装是该web应用的信息,Servle ...

  9. NHibernate N+1问题实例分析和优化

    1.问题的缘起 考察下面的类结构定义 public class Category { string _id; Category _parent; IList<Category> _chil ...

  10. 大数据学习——scala入门练习

    package com /** * Created by ZX on 2015/11/6. */ object VariableDemo { def main(args: Array[String]) ...