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之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- innerxml and outerxml
xml文件如下 <root> <a></a> <b></b> <c></c> <a></a> ...
- ConfigurationManager.AppSettings Property
在app.config文件中添加如下配置 <appSettings> <add key="Server" value="127.0.0.1"/ ...
- native2ascii运用
1.native2ascii命令行的格式 native2ascii -[option] [inputfile [outputfile]] 说明: -[option]:表示命令开关,有两个选项可供选择: ...
- Akka源码分析-深入ActorRef&ActorPath
上一节我们深入讨论了ActorRef等相关的概念及其关系,但ActorRef和ActorPath的关系还需要再加以分析说明.其实还是官网说的比较清楚. “A path in an actor syst ...
- javascript中window,document,body的解释
解释javascript中window,document,body的区别: window对象表示浏览器中打开的窗口,即是一个浏览器窗口只有一个window对象. document对象是载入浏览器的ht ...
- C# Autofac 出现 尝试创建“XXController”类型的控制器时出错。请确保控制器具有无参数公共构造函数 错误解决方案
出现以下错误: 总结解决方案: 本项目采用构造函数方法进行依赖注入,由于个人原因在业务层相互注入了接口,导致交叉:报错
- Mysql(Innodb)如何避免幻读
Mysql(Innodb)如何避免幻读 有意思 MySQL InnoDB支持三种行锁定方式: 行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. 间隙锁(Gap Lock):锁定 ...
- linux对比两个文件的差异
在项目维护阶段,经常会对垃圾文件进行清理.比如没有在数据库中的文件进行删除,这个时候最好的选择就是使用shell命令了:废话不多说直接上代码: 1.首先准备好从数据表导出来的数据,方法随意 2.在服务 ...
- String字符串的完美度
题目详情: 我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同, 而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度 ...
- 【转】Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...