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之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
随机推荐
- AngularJS2.0 一个表单例子——总体说来还是简化了1.x 使用起来比较自然
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)
题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...
- spring基础学习---简单配置文件
- weak看iOS面试
2013年 面试官:代理用weak还是strong? 我 :weak . 面试官:明天来上班吧 2014年 面试官:代理为什么用weak不用strong? 我 : 用strong会造成循环引用. 面试 ...
- bootstrap图标乱码问题-解决方案
楼主在使用bootstrap时,出现了图标乱码问题,经过多次查找,才解决了问题(最后发现真的是很好解决的问题(^^)) 如果出现乱码 请直接在自己写的CSS中重新引入一下font文件中的字体就好了 @ ...
- win32绘图基础
获取设备环境句柄: (1)WM_PAINT消息中: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); EndPaint(hwnd,&ps ...
- Resources.getResourceAsReader 报空指针
1.maven项目中 可能未编译 导致找不到, 解决方法:mvn clean install -DskipTests -X 编译一下项目 2.可能在 Resources.getResourceAs ...
- java攻城师之路--复习java web之servlet
需要掌握的知识点:1.Servlet程序编写 ----- 生命周期2.ServletAPI Request Response 3.Cookie 和 Session Servlet 用来 动态web资源 ...
- sql 分析 依赖beanutils
你还在为sql语句的拼接而烦恼吗? sql语句支持表达式了! package com.newland.bi.webservice.common.manage; import java.util.Arr ...
- MyEclipse中VSS的使用详解
本文系转载,原文地址http://hi.baidu.com/yi88cheng/blog/item/13dd862f765e6b5c4fc226e5.html