原题链接: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 == '0') {if (c != '?')a = c;return true;}else if (b == '1') {if (c != '?')a = '1' + '0' - c;return true;}}else if (a == '0') {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 = '1' + '0' - c;if (c == '?' && b != '?')c = '1' + '0' - b;return true;}
}int main() {scanf("%s%s", k, x);bool now = 0;int n = strlen(k);for (int i = 0; i < n; i++) {char tmp = '0';bool flag = true;if (i == 0)flag = deal(tmp, k[i], x[i]);else flag = deal(k[i - 1], k[i], x[i]);if (!flag) {cout << "Impossible" << endl;return 0;}}for (int i = n - 1; i >= 0; i--) {char tmp = '0';bool flag = true;if (i == 0)flag = deal(tmp, k[i], x[i]);else flag = deal(k[i - 1], k[i], x[i]);if (!flag) {cout << "Impossible" << endl;return 0;}}bool flag = true;for (int i = 0; i < n; i++)if (x[i] == '?') {flag = false;break;}for (int i = 0; i < n; i++)if (k[i] == '?') {flag = false;break;}if (flag)printf("%s\n%s", k, x);else printf("Ambiguity\n");return 0;
}

转载于:https://www.cnblogs.com/HarryGuo2012/p/4728184.html

Ural 1780 Gray Code 乱搞暴力相关推荐

  1. AcWing 397. 逃不掉的路(边双连通分量缩点成树 + 树链剖分乱搞)

    整理的算法模板合集: ACM模板 我们知道在同一个边双连通分量中的点没有必经边(因为至少有两条分离的路径). 所以我们直接tarjan求出桥后缩点,然后求一下树上两点间的距离即可. 那么如何求树上两点 ...

  2. BZOJ-1800 飞行棋 数学+乱搞

    这道题感觉就是乱搞,O(n^4)都毫无问题 1800: [Ahoi2009]fly 飞行棋 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1172 So ...

  3. 【BZOJ-3578】GTY的人类基因组计划2 set + map + Hash 乱搞

    3578: GTY的人类基因组计划2 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 367  Solved: 159 [Submit][Status ...

  4. BZOJ4401:块的计数(乱搞)

    Description 小Y最近从同学那里听说了一个十分牛B的高级数据结构--块状树.听说这种数据结构能在sqrt(N)的时间内维护树上的各种信息,十分的高效.当然,无聊的小Y对这种事情毫无兴趣,只是 ...

  5. Luogu3732 [HAOI2017] 供给侧改革 【后缀数组】【线段树】【乱搞】

    题目分析: 这道题我是乱搞的,因为他说$01$串是随机的. 那么我们可以猜测能够让LCP变大的地方很少.求出后缀数组之后可能让LCP变大的地方就等价于从大到小往height里动态加点同时维护这个点左右 ...

  6. 【BZOJ-2937】建造酿酒厂 前缀和 + 展环为链 + 乱搞

    2937: [Poi2000]建造酿酒厂 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 70  Solved: 24 [Submit][Status] ...

  7. 【uoj#142】【UER #5】万圣节的南瓜灯 乱搞+并查集

    题目描述 给出一张 $n\times m$ 的网格图,两个格子之间有一条双向边,当且仅当它们相邻,即在网格图中有一条公共边. 特殊地,对于 $1\le x\le n​$ ,$(x,1)​$ 和 $(x ...

  8. hash进阶:使用字符串hash乱搞的姿势

    前言 此文主要介绍hash的各种乱搞方法,hash入门请参照我之前这篇文章 不好意思hash真的可以为所欲为 在开头先放一下题表(其实就是我题解中的hash题目qwq) 查询子串hash值 必备的入门 ...

  9. 【2021牛客暑期多校训练营5】Jewels(建图,最小匹配权,KM乱搞)

    J Jewels 题意: 给出n个点的位置x,y,z以及速度v,从第t=0秒开始打捞,打捞点i的代价为x^2+y^2+(z+t*v)^2,求打捞所有点的最小代价. 思路: 所有的宝石肯定都在 0~n- ...

最新文章

  1. 轻松破解NewzCrawler时间限制
  2. AngularJS之动态菜单操作指令
  3. Android分级部门选择界面(二)
  4. html语言hr的用法,HTML hr noshade 属性 | Paoo教程
  5. 浅析Java.lang.Runtime类
  6. 吃下去的东西老是往上翻上来_这间日日火爆的面馆,当年海藻带宋思明吃过
  7. ASP.NET没有魔法——ASP.NET MVC 与数据库之MySQLEF
  8. unity热更新json_Unity热更新之AssetBundle打包篇
  9. u-boot移植重要问题说明
  10. innerHTML,innerText
  11. Eclipse通过JDWP调试Dalvik
  12. [Umbraco] 创建第一个页面
  13. split分片主要源码解析
  14. 武汉数字工程研究所计算机软件分数,武汉数字工程研究所2017考研成绩查询时间:2月16日...
  15. android 各个版本代表图标,使用不同的Android SDK版本的不同图标
  16. rest 怎么发送html,docusignapi - 是否可以使用REST API中的HTML创建Docusign模板? - 堆栈内存溢出...
  17. NGUI_2.6.3_系列教程三
  18. Android 程序随开机自动运行
  19. 一淘网是马云手中的防守牌
  20. 金蝶KIS专业版“登录时出现问题,请重新输入”终极解决全过程

热门文章

  1. 数据库里的小知识❤️
  2. “我升职的原因是:比同事更早学会这个知识”
  3. java如何读取文本,Java:如何读取文本文件
  4. cd如何省略空格 linux_在 Linux 上调整命令历史 | Linux 中国
  5. tostring会空指针吗_追了多年的开发框架,你还认识指针吗?
  6. 低代码开发平台_如何挑选最适合你的低代码开发平台
  7. 模型算法_生产建模型 算法控效率
  8. Linux如何创建一个内核模块,创建你自己的内核模块
  9. java线程入门_java多线程快速入门(一)
  10. positive definite quadratic form and positive definite matrix