G(x)

Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 184    Accepted Submission(s): 44

Problem Description
For a binary number x with n digits (AnAn-1An-2 ... A2A1), we encode it as 
Where "" is bitwise XOR operation and "" indicates the largest integer which is not greater than x.
Due to some reasons, Mzry1992 encode his password P into G(P), and additionally, he encode P + 1 into G(P + 1) too, and write G(P) and G(P + 1) into his diary.
This story happened many years ago and now you hold the diary 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 has a number T (T <= 100) , indicating the number of test cases.
For every test case, it has 2 lines of same number of digits describe G(P) and G(P + 1), In every line, it only contains 1, 0 and ?. Unreadable digits are denoted with symbol ?, The length of every line in the input is up to 105.
 
Output
For every case, you should output "Case #t: " at first, without quotes. The t is the case number starting from 1.
Then, if there is impossible to restore G(P) and G(P + 1), you should output "Impossible" in the second line.
Otherwise, if G(P) is unique, you should output restored G(P) and G(P +1) in the same format.
Otherwise, you should output "Ambiguous" and the number of possible G(P) in the second line.
The number may be very large so the answer should modulo 10^9 + 7.
 
Sample Input
3
10??
10??
0010
0110
1?01
0?01
 
Sample Output
Case #1:
Ambiguous 3
Case #2:
0010
0110
Case #3:
Impossible

Hint

In the first sample case, the three possible situations are:
1.
G(12) = 1010
G(13) = 1011
2.
G(13) = 1011
G(14) = 1001
3.
G(14) = 1001
G(15) = 1000

 
Source
 
Recommend
liuyiding
 

很容易找出规律,

然后递推,枚举就解决了。

P和P+1 其实就是差后面一点

P: XXXXX  0   1    1     1

P+1: XXX  1   0    0     0

关键点就是枚举P的第一个0的位置。左边的P和P+1是一样的。

 /* ***********************************************
Author :kuangbin
Created Time :2013/9/14 星期六 14:16:53
File Name :2013成都网络赛\1006.cpp
************************************************ */ #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
const int MOD = 1e9+;
char str1[MAXN], str2[MAXN];
int dp[MAXN][];
int flag[MAXN][]; char sss1[MAXN],sss2[MAXN]; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
printf("Case #%d:\n",iCase);
scanf("%s%s",str1,str2);
int n = strlen(str1);
dp[][] = ;
dp[][] = ;
flag[][] = ;
flag[][] = ;
for(int i = ;i <= n;i++)
{
if(str1[i-] == '?' && str2[i-] == '?')
{
dp[i][] = dp[i-][] + dp[i-][];
dp[i][] = dp[i-][] + dp[i-][];
int tmp = flag[i-][] + flag[i-][];
if(tmp == )
flag[i][] = flag[i][] = ;
else if(tmp == )
flag[i][] = flag[i][] = ;
else flag[i][] = flag[i][] = ;
}
else if(str1[i-] == '?' || str2[i-] == '?')
{
if(str1[i-] == '' || str2[i-] =='')
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][];
}
else if(str1[i-] == '' || str2[i-] =='')
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][];
}
}
else if(str1[i-] != str2[i-])
{
dp[i][] = dp[i][] = ;
flag[i][] = flag[i][] = ;
}
else
{
if(str1[i-] =='')
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][]; }
else
{
dp[i][] = dp[i-][];
dp[i][] = dp[i-][];
flag[i][] = flag[i-][];
flag[i][] = flag[i-][];
}
}
if(dp[i][] >= MOD)dp[i][] -= MOD;
if(dp[i][] >= MOD)dp[i][] -= MOD;
}
int ans = ;
int flag_num = ;
int ss_id = -;
for(int i = n;i > ;i--)
{
if(i+ <= n)
{
if(str1[i+] == '' || str2[i+] == '')
break;
}
if(i+ <= n)
{
if(str1[i] == '' || str2[i] == '')
continue;
}
if(i- >= )
{
if(str1[i-] == str2[i-] && str1[i-] != '?')
continue;
}
char ch;
if(str1[i-] == str2[i-] && str1[i-] == '?')
{
ans += dp[i-][];
ans %= MOD;
ans += dp[i-][];
ans %= MOD;
flag_num += flag[i-][] + flag[i-][];
if(flag_num > )flag_num = ;
}
else
{
if(str1[i-] != '?')ch = str1[i-];
else
{
if(str2[i-] == '')ch = '';
else ch = '';
}
if(ch == '')
{
ans += dp[i-][];
ans %= MOD;
flag_num += flag[i-][];
if(flag_num > )flag_num = ;
}
else
{
ans += dp[i-][];
ans %= MOD;
flag_num += flag[i-][];
if(flag_num > )flag_num = ;
}
}
if(flag_num == && ss_id == -)ss_id = i;
}
if(flag_num == )
{
printf("Impossible\n");
continue;
}
if(flag_num > )
{
printf("Ambiguous %d\n",ans);
continue;
}
if(flag_num == )
{
sss1[n] = sss2[n] = ;
sss1[ss_id-] = '';
sss2[ss_id-] = '';
for(int i = ss_id;i < n;i++)
sss1[i] = '', sss2[i] = '';
int last = ;
for(int i = ss_id-;i >= ;i--)
{
int now;
if(flag[i+][] > )now = ;
else now = ;
sss1[i] = sss2[i] = '' + now; }
for(int i = n-; i > ;i--)
{
if(sss1[i-] ==sss1[i])sss1[i] = '';
else sss1[i] = '';
if(sss2[i-] == sss2[i])sss2[i] = '';
else sss2[i] = '';
} printf("%s\n%s\n",sss1,sss2);
continue;
}
cout<<ans<<endl; }
return ;
}

HDU 4733 G(x) (2013成都网络赛,递推)的更多相关文章

  1. HDU 4730 We Love MOE Girls (2013成都网络赛,签到水题)

    We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4737 A Bit Fun (2013成都网络赛)

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 4734 F(x) (2013成都网络赛,数位DP)

    F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU 4731 Minimum palindrome (2013成都网络赛,找规律构造)

    Minimum palindrome Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. HDU 4747 Mex (2013杭州网络赛1010题,线段树)

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  6. HDU 4768 Flyer (2013长春网络赛1010题,二分)

    Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. HDU 4737 A Bit Fun 2013成都 网络赛 1010

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4737 题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i, ...

  8. 2013成都网络赛 C We Love MOE Girls(水题)

    We Love MOE Girls Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. 2013成都网络赛 J A Bit Fun(水题)

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. [BZOJ 2299][HAOI 2011]向量 题解(裴蜀定理)

    [BZOJ 2299][HAOI 2011]向量 Description 给你一对数a,b,你可以任意使用(a,b), (a,-b), (-a,b), (-a,-b), (b,a), (b,-a), ...

  2. 第9月第16天 tolua++ cocos2dx cocos2d-lua

    1. http://www.jianshu.com/p/1cdfc60da04f 2.lua c++ Lua访问C++类 现在,我们在Lua里面操作这个Student类.注意,我们绑定的每一个函数都需 ...

  3. 使用 scm-manager 搭建 git/svn 代码管理仓库(一)

    1.在官网上下载scm-manager 下载地址  https://www.scm-manager.org/download/ 选择下载文件 2. 配置java 环境 参照文章:https://jin ...

  4. c++ 函数指针简单实例

    一开始看函数指针的时候我是很懵的,因为不知道它有什么用,之后慢慢就发现了自己的愚昧无知. 假设我们想实现一个数据结构,比如二叉搜索树,堆.又或者是一个快排,归并排序. 我们一般是直接在两个数要比较的时 ...

  5. imperva代理拦截

    <external-traffic-monitoring-in-kern>true</external-traffic-monitoring-in-kern> 添加这段就可以开 ...

  6. 在Windows环境中利用Responder工具窃取NTLMv2哈希

    在Windows环境中利用Responder工具窃取NTLMv2哈希 翻译自:https://github.com/incredibleindishell/Windows-AD-environment ...

  7. 百度编辑器ueditor 字符限制

    百度编辑器ueditor 字符限制 默认只能输入10000个字符 修改 ueditor.config.js // ,wordCount:true //是否开启字数统计 // ,maximumWords ...

  8. 014_mac下的端口查看

    一. 使用netstat去过滤listen效果不怎么理想. $ netstat -an|grep -i --color "listen" tcp6 0 0 ::1.5601 *.* ...

  9. cas:覆盖安装

    1.首先到github上下载最新的模板代码 https://github.com/apereo/cas-overlay-template 下载完成后,导入该工程. 2.编译打包 cd cas-over ...

  10. Newtonsoft 反序列化字符串

    string json=“[{“name”:”zhangsan”,”age”:”12”},{“name”:”zhangsan”,”age”:”12”}]” 方法1: JArray arr = (JAr ...