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之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- 不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转)
不仅开源,而且对企业应用完全免费!ExtAspNet弃用GPL v2,拥抱Apache License 2.0(转) 提出问题 ExtAspNet开源以来,一直坚持开源免费的原则,但是其GPL v2的 ...
- 【HDU 4864】 Task
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4864 [算法] 贪心 不妨将两个数组分别按x从大到小排序 然后枚举每件物品,选择x值大于该物品的且 ...
- LuoguP3261 [JLOI2015]城池攻占
题目描述 小铭铭最近获得了一副新的桌游,游戏中需要用 m 个骑士攻占 n 个城池.这 n 个城池用 1 到 n 的整数表示.除 1 号城池外,城池 i 会受到另一座城池 fi 的管辖,其中 fi &l ...
- 杂项:BPM
ylbtech-杂项:BPM 1.返回顶部 1. BPM,即业务流程管理,是一种以规范化的构造端到端的卓越业务流程为中心,以持续的提高组织业务绩效为目的的系统化方法,常见商业管理教育如EMBA.MBA ...
- android sqlite中判断某个表是否存在
<span style="font-size:18px;">sqlite 中判断某个表是否存在的方法,贴出来供大家参考 /** * 判断某张表是否存在 * @param ...
- Coursera公开课-Machine_learing:编程作业7
这周的编程作业主要是两方面内容. 1.K-means聚类. 2.PCA(Principle Component Analys)主成分分析. 方式主要是通过对图像的聚类实现压缩图像,后来发现PCA也可以 ...
- Elasticsearch之CURL命令的PUT和POST对比
PUT是幂等方法,而POST并不是. PUT用于更新操作,POST用于新增操作比较合适. PUT,DELETE操作是幂等的,所谓幂等就是指不管进行多少次操作,结果都一样. 比如,我用PUT修改一篇文章 ...
- debounce还是throttle(去抖和节流)
debounce 去抖 我的理解很简单,比方说window.onscroll会疯狂触发handler,此时给它一个debounce(handler, delayTime). 就是不管你延时时间内触发了 ...
- Ubuntu16安装jdk8配置Tomcat9
一.配置jdk 1.下载解压是肯定不能少的 2.配置环境变量根据自己需求来 export JAVA_HOME=/usr/software/jdk1.8.0_121 export CLASSPATH=. ...
- vue iView 打包后 字体图标不显示
问题描述: 今天webpack打包后发现iView 字体图标不显示 解决方案: build/webpack.prod.conf.js 这个文件里面 module: { rules: utils.sty ...