原题链接: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. day12-迭代器

    迭代器的概念 内部含有_next_和_iter_方法的就是迭代器. 可以被for循环的都是可迭代的,只有是可迭代对象,才能用for循环. 可迭代的内部都有_iter_方法——可迭代协议. 只要是迭代器 ...

  2. LeetCode(217)Contains Duplicate

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  3. URAL - 2065 Different Sums (思维题)

    题意: 给n和k,让你用不小于 k 个不同的数字构成一个长度为n的序列,使得序列中不同的区间和的数目最小. n,k<=500 k-1个数填一些数字的一正一负,这样有些区间和为0. 剩下的都填0. ...

  4. .Net Task常见问题

    最近尝试使用一下Task,但是使用过程中因为API的不熟悉碰到了很多问题,不清楚什么时间来调用Task.Start(),具体该怎么使用等等. 如下所描述的Task.Start()方法均为实例方法. 1 ...

  5. P3369 【模板】普通平衡树 Treap

    P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...

  6. nginx的常用负载均衡算法,分别是

    随机分配,hash一致性分配,最小连接数分配,主备分配 随机,轮训,一致性哈希,主备,https://blog.csdn.net/liu88010988/article/details/5154741 ...

  7. jquery如何获取某一个兄弟节点

    $('#id').siblings() 当前元素所有的兄弟节点 $('#id').prev() 当前元素前一个兄弟节点 $('#id').prevaAll() 当前元素之前所有的兄弟节点 $('#id ...

  8. MyBatis拦截器打印不带问号的完整sql语句方法

    /* Preparing: SELECT * FROM tb_user WHERE id = ? AND user_name = ?  目标是打印:SELECT * FROM tb_user WHER ...

  9. 测试jsonp

    login<?php $type = $_GET['type']; if(empty($type)) { $url = ""; }else { if($type == 'lo ...

  10. Kafka介绍 (官方文档翻译)

    摘要:Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据. 这种动 ...