Google Code Jam 2016 Round 1B B
题意:给出两个数字位数相同,分别中间有若干位不知道,用问号表示。现在要求补全这两个数字,使得差值的绝对值最小,多解则取第一个数字的值最小的,再多解就取第二个数字最小的。
分析:
类似数位dp,但是很多状态可以直接得出最终解,个别状态需要状态转移。
我们从高位到低位依次确定两个数的每个位是几。一旦确定了两个数的一个位不一样,则可以立即将小的一方的后续问号全部写9,大的一方后续问号全部写0。这样才能让差值最小。
那我们观察每个位的时候要如何确定其值呢?分如下几种情况。
1.两个数的该位都是问号,那么分三种情况:
1.1 都标为0,看下一个位。
1.2&1.3 将一位标为1,另一个位标为0,并更新答案,终结状态。(这表示我们主动在高位制造了一个差值以便后面取得整体差值最小。例如:?0?,?9?,答案是100,099)
2.两个数的该位都不是问号,若相等,继续看下一位。若不等,则标出后面问号,更新答案,终结状态。
3.两个数的该位有一个是问号,那么这个这与第一种情况类似,分三种情况处理。
3.1 标为与该位相等,看下一个位。
3.2 标为该位减1,赋值后续问号,更新答案,终结状态。
3.3 标为该位加1,赋值后续问号,更新答案,终结状态。
就这么多情况。值得注意的是,虽然有些时候进行了状态转移(看下一个位)。但是并不需要搜索和回溯。因为每个状态的多个分支中,只有一个是状态转移,其他的都是最终态。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std; #define d(x) const int MAX_LEN = ; char st[][MAX_LEN];
char ans[][MAX_LEN];
char temp[][MAX_LEN];
long long diff;
int n; void input()
{
scanf("%s", st[]);
scanf("%s", st[]);
n = strlen(st[]);
} long long get_value(char st[])
{
long long ten = ;
long long ret = ;
for (int i = n - ; i >= ; i--)
{
ret += (st[i] - '') * ten;
ten *= ;
}
return ret;
} bool ok(long long temp_diff)
{
if (temp_diff > diff)
return false;
if (temp_diff < diff)
return true;
if (strcmp(ans[], temp[]) > )
return true;
if (strcmp(ans[], temp[]) < )
return false;
return strcmp(ans[], temp[]) > ; } void update()
{
long long a = get_value(temp[]);
long long b = get_value(temp[]);
long long temp_diff = abs(a - b);
if (ok(temp_diff))
{
diff = temp_diff;
strcpy(ans[], temp[]);
strcpy(ans[], temp[]);
}
} void make(int index, int a, int b)
{
if (a < || b < || a > || b > )
return; strcpy(temp[], st[]);
strcpy(temp[], st[]); temp[][index] = a + '';
temp[][index] = b + ''; int ch_a = '';
int ch_b = '';
if (a > b)
swap(ch_a, ch_b);
for (int i = index + ; i < n; i++)
{
if (temp[][i] == '?')
temp[][i] = ch_a;
if (temp[][i] == '?')
temp[][i] = ch_b;
}
d(printf("a=%d\n", a));
d(printf("b=%d\n", b));
d(puts(temp[]));
d(puts(temp[]));
update();
} void work()
{
diff = 1LL << ;
for (int i = ; st[][i]; i++)
{
if (st[][i] == st[][i] && st[][i] != '?')
{
continue;
}
if (st[][i] == st[][i] && st[][i] == '?')
{
make(i, , );
make(i, , );
st[][i] = st[][i] = '';
continue;
}
//reach here means st[0][i] != st[1][i]
if (st[][i] != '?' && st[][i] != '?')
{
make(i, st[][i] - '', st[][i] - '');
return;
}
//reach here means only one of them is ?.
if (st[][i] == '?')
{
make(i, st[][i] - '' + , st[][i] - '');
make(i, st[][i] - '' - , st[][i] - '');
st[][i] = st[][i];
}
if (st[][i] == '?')
{
make(i, st[][i] - '', st[][i] - '' + );
make(i, st[][i] - '', st[][i] - '' - );
st[][i] = st[][i];
} }
make(n - , st[][n - ] - '', st[][n - ] - '');
} int main()
{
int t;
scanf("%d", &t);
int case_num = ;
while (t--)
{
case_num++;
printf("Case #%d: ", case_num);
input();
work();
printf("%s %s\n", ans[], ans[]);
}
return ;
}
Google Code Jam 2016 Round 1B B的更多相关文章
- Google Code Jam 2016 Round 1B Problem C. Technobabble
题目链接:https://code.google.com/codejam/contest/11254486/dashboard#s=p2 大意是教授的学生每个人在纸条上写一个自己的topic,每个to ...
- Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks
https://code.google.com/codejam/contest/635101/dashboard#s=p1 Problem A flock of chickens are runn ...
- Google Code Jam 2010 Round 1B Problem A. File Fix-it
https://code.google.com/codejam/contest/635101/dashboard#s=p0 Problem On Unix computers, data is s ...
- Google Code Jam 2016 Round 1C C
题意:三种物品分别有a b c个(a<=b<=c),现在每种物品各选一个进行组合.要求每种最和最多出现一次.且要求任意两个物品的组合在所有三个物品组合中的出现总次数不能超过n. 要求给出一 ...
- Google Code Jam 2014 Round 1B Problem B
二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring& ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
- Google Code Jam 2010 Round 1C Problem A. Rope Intranet
Google Code Jam 2010 Round 1C Problem A. Rope Intranet https://code.google.com/codejam/contest/61910 ...
- [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha
Problem B. Cookie Clicker Alpha Introduction Cookie Clicker is a Javascript game by Orteil, where ...
- [Google Code Jam (Qualification Round 2014) ] A. Magic Trick
Problem A. Magic Trick Small input6 points You have solved this input set. Note: To advance to the ...
随机推荐
- Xtrabackup原理及使用innobackupex进行MySQL数据库备份恢复
Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具. Xtrabackup中主要包含两个工 ...
- Django进阶(三)
ORM 众所周知有很多不同的数据库系统,并且其中的大部分系统都包含Python接口,能够让我们更好的利用它们的功能,而这些系统唯一的缺点就是需要你了解SQL,如果你是一个更愿意操纵Python对象,而 ...
- UVA232
这只是大概的雏形. 步骤就是:1输入网格,2给网格里的起始格编序号,3输出所有字母,前面要加序号 #include<stdio.h> #include<ctype.h> #in ...
- 安装第三方RPM仓库
1.安装RepoForge源: CentOS 6.x [root@localhost /]# yum install http://pkgs.repoforge.org/rpmforge-releas ...
- TableView 滑动收起键盘
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 拖拽tableView就会收起键盘
- 解决Windows版Git出现templates not found的问题
环境: Win10 x64 Git windows客户端(下载自 https://git-scm.com/) SourceTree 1.9.6.1(使用系统安装的Git,而非SourceTree内嵌的 ...
- php实现文件上传与下载(上)
php实现文件的上传与下载是一个挺基本的功能,一般网站多多少少都会有这样的需求在内,当然不是说所有的文件都可以被上传,那这网络就太没有安全性可言了.因为接触php时间不长,今天写练练手,随笔也就是公开 ...
- 首次接触nodejs
嗯,2017年第一次接触nodejs ,也费了一些时间才终于将hello world正确运行出来. 下面说一下我的详情吧: 第一步:不用说,在https://nodejs.org/en/下载一款新的稳 ...
- spring boot + swagger + mysql + maven
1.首先编写 yaml 文件,创建项目所需的接口,在swagger.io官网上生成 spring boot项目: 2.由于生成的spring boot项目是公共类的所以还需要修改成所需的项目名称,主要 ...
- web页面之响应式布局
一.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网 ...