Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression
题意:
有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数
给出这个数,要求还原等式,length <= 1e6
三个数不能含有前导0,保证有解
解法:
铁头过题法,分类然后各种判断
我分了5种情况
0.开头字符为0, 那么结果一定是0+a=a的形式
然后4种情况
1.len(a) >= len(b) 且 len(c) == len(a)
2.len(a) <= len(b) 且 len(c) == len(b)
3.len(a) >= len(b) 且 len(c) == len(a) + 1
4.len(a) <= len(b) 且 len(c) == len(b) + 1
前两种(没有进位)判断的情况:
(1)三个数如果长度不为1,那么最高位不能为0
(2)len(a) != len(b) 时, 若max(a, b)的最高位为x,
则c的最高位,应该为x或者x+1
(3)满足上面两种情况,开始检验a+b的每一位与c的每一位是否相等
后两种(有进位)判断的情况:
(1)三个数如果长度不为1,那么最高位不能为0
(2)c的最高位必须是1
(3)满足上面两种情况,开始检验a+b的每一位与c的每一位是否相等
时间复杂度未知,但是实际运行效果,时空复杂度都是超赞的!
(整理了一下代码似乎精简过度影响了可读性了...)
#include <stdio.h>
#include <string.h> int n; char s[]; int i, j, k, p, l, r; int print() {
int i = ;
for (; i <= l; i ++)
putchar(s[i]);
putchar('+');
for (; i <= r; i ++)
putchar(s[i]);
putchar('=');
for (; i <= n; i ++)
putchar(s[i]);
return ;
} int check(int s1, int s2) {
for (int i = s1, j = s2, k = n, t = ; k > s2; k --) {
if ((s[i] + s[j] + t - ) % != s[k] - )
return ;
t = (s[i] + s[j] + t - ) / ;
i --, j --;
if (i <= ) i = ;
if (j <= s1) j = ;
}
return ;
} int judge() {
if (l + != r && s[l + ] == '') return ;
if (p) {if (s[r + ] != k) return ;}
else if (i != j && (k != s[r + ] && k + != s[r + ])) return ;
if (!check(l, r)) return ;
return print();
} int main() {
gets(s + );
n = strlen(s + );
s[] = ''; l = , r = n - n / ;
if (s[] == '') {
print();
return ;
} for (i = + (n & ? : ), j = (n - i) / , r = n - j; i <= n / ; i += , j --, r ++) {
l = j, k = s[];
if (judge()) return ;
l = i, k = s[l + ];
if (judge()) return ;
} p = , k = ;
for (i = + (n & ? : ), j = (n - i) / , r = n - j - ; i <= n / - (n % == ); i += , j --, r ++) {
l = j;
if (judge()) return ;
l = i;
if (judge()) return ;
}
}
Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression的更多相关文章
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #451 (Div. 2) A B C D E
Codeforces Round #451 (Div. 2) A Rounding 题目链接: http://codeforces.com/contest/898/problem/A 思路: 小于等于 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces 898F - Restoring the Expression(字符串hash)
898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...
- Codeforces Round #451 (Div. 2) [ D. Alarm Clock ] [ E. Squares and not squares ] [ F. Restoring the Expression ]
PROBLEM D. Alarm Clock 题 OvO http://codeforces.com/contest/898/problem/D codeforces 898d 解 从前往后枚举,放进 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- 交换排序(2)——冒泡排序(bubble sort)
冒泡排序算法的运作如下:(从后往前) 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 针对所 ...
- go语言笔记——包的概念本质上和java是一样的,通过大小写来区分private,fmt的Printf不就是嘛!
示例 4.1 hello_world.go package main import "fmt" func main() { fmt.Println("hello, wor ...
- StackExchange.Redis 使用资料
在StackExchange.Redis中最重要的对象是ConnectionMultiplexer类, 它存在于StackExchange.redis命名空间中.这个类隐藏了Redis服务的操作细节, ...
- 重装mysql
重装mysql方法. 转自http://blog.sina.com.cn/s/blog_73000beb01012eh4.html 1.删除 mysql 1.1 sudo apt-get autore ...
- openstack 杂记 备忘
- 63.ExtJs事件(自定义事件、on、eventManager)示例
转自:https://blog.csdn.net/leadergg/article/details/5927614?utm_source=blogxgwz5 ExtJs事件(自定义事件.on.even ...
- MySQL 1366错误解决办法
MySQL 1366错误大致描述如下 SQL Error: 1366: Incorrect string value: "xE8xAFxA6xE7xBBx86-" for colu ...
- MAC地址 初识
MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...
- 用JavaScript实现歌词滚动播放
各种音乐播放器上都有一个自动滚动播放歌词的功能,那么这个功能用JavaScript怎么实现呢?请看下文. 一般音乐播放器使用的歌词格式都是lrc,为了方便处理,我们这里使用XML格式的歌词.介绍一个网 ...
- ASP.NET MVC5 之路由器
这篇博客介绍的很详细 http://www.cnblogs.com/yaozhenfa/p/asp_net_mvc_route_1.html