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. 20155225 2016-2017-2 《Java程序设计》第八周学习总结

    20155225 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 通用API 日志API 国际化基础 规则表达式 JDK8增强功能 教材学习中的问题和解决过程 ...

  2. 收集SpringBoot的一些学习资料

    1:wuyouzhuguli的博客  (24篇) https://github.com/wuyouzhuguli/Spring-Boot-Demos 2:方志鹏的博客  (27篇) https://b ...

  3. c++刷题(43/100)矩阵旋转打印

    题目1:矩阵旋转打印 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则 ...

  4. aps.net webform框架下页面服务器端控件和html控件用法

    (1)select 下拉框 前端: <select name="gameserverlist" id="gameserverlist" runat=&qu ...

  5. Linux下JDK到底应该安装在哪儿?

    1 Linux 目录结构 即使这是个菜鸟级的问题,也经常难住老鸟.我就见过很资深的程序员把JDK不合适地安装到/home目录下.虽然不一定有最正确的安装位置,但一定有不适当的安装位置.为了确定我们到底 ...

  6. find查找时排除目录及文件

    查找根目录下大于500M的文件,排除/proc目录 find / ! -path "/proc/*" -type f -size +500M | sort -rh|xargs ls ...

  7. C#使用mybatis学习笔记

    1.官网:http://mybatis.org/index.html 2.代码:https://code.google.com/p/mybatisnet/ 3.wiki:http://zh.wikip ...

  8. High-Speed Tracking with Kernelized Correlation Filters

          2015年的一篇论文,可参考:http://blog.csdn.net/carrierlxksuper/article/details/46461245.      另参考:http:// ...

  9. springMVC源码分析--视图View(一)

    之前的博客springMVC源码分析--HttpMessageConverter数据转化(一)中我们已经介绍了数据返回值的处理,在博客springMVC源码分析--ViewResolver视图解析器( ...

  10. CI框架中自带的加密解密如何应用

    首先我们找到配置文件application/config/config.php  ,找到如下代码: ? 1 $config['encryption_key'] = "YOUR KEY&quo ...