Ural 1780 Gray Code 乱搞暴力
2015-08-13 18:51
453 查看
原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780
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.
Create a 2-bit list: {0, 1}.
Reflect this list and concatenate it with the original list: {0, 1, 1, 0}.
Prefix old entries with 0, and prefix new entries with 1: {00, 01, 11, 10}.
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?
1780. Gray Code
Time limit: 0.5 secondMemory 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.
Create a 2-bit list: {0, 1}.
Reflect this list and concatenate it with the original list: {0, 1, 1, 0}.
Prefix old entries with 0, and prefix new entries with 1: {00, 01, 11, 10}.
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; }
相关文章推荐
- MySQL复制表数据到新表的方法
- vim的安装和使用
- WCF—Binding
- pat 1096
- jquery记忆笔记
- 微博的开发
- 【Head First Java 读书笔记】(三)primitive主数据类型和引用
- jquery记忆笔记
- iOS 开发小技巧
- 使用Sandcastle生成CHM帮助文档
- IDEA下使用Jetty进行Debug模式调试
- Gym 100625H 多边形面积-计算几何
- java dom解析xml
- CMD方式修改MySQL的root用户密码
- 图片下载的作业
- (2.1.3.1)Linux文件权限详解
- 如何用Maven创建web项目(具体步骤)
- iOS FMDB 查询 批量更新
- oracle rac service学习
- window环境下安装 pip 工具 【pip为Python的扩展管理工具】