题意:

有一个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的更多相关文章

  1. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  2. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  3. Codeforces Round #451 (Div. 2) A B C D E

    Codeforces Round #451 (Div. 2) A Rounding 题目链接: http://codeforces.com/contest/898/problem/A 思路: 小于等于 ...

  4. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  5. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  6. Codeforces 898F - Restoring the Expression(字符串hash)

    898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...

  7. 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 解 从前往后枚举,放进 ...

  8. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

  9. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

随机推荐

  1. 2243: [SDOI2011]染色(树链剖分+线段树)

    2243: [SDOI2011]染色 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 8400  Solved: 3150[Submit][Status ...

  2. 【翻译】- EffectiveAkka-第二章(一)

    Actor使用模式 现在我们已经了解了可以创建的actor系统的不同类型,那么我们在编写基于actor的应用程序时,可以采用什么样的使用模式,以便避免出现常见错误呢? 下面就让我们看看其中使用模式. ...

  3. JavaScript--确认(confirm 消息对话框)

    confirm 消息对话框通常用于允许用户做选择的动作,如:“你对吗?”等.弹出对话框(包括一个确定按钮和一个取消按钮). 语法: confirm(str); 参数说明: str:在消息对话框中要显示 ...

  4. android:scaleType 布局文件加载图片时候的显示方式

    android:scaleType="center" 以原图的几何中心点和ImagView的几何中心点为基准,按图片的原来size居中显示,不缩放,当图片长/宽超过View的长/宽 ...

  5. Python 必选参数,默认参数,可变参数,关键字参数和命名关键字参数

    Py的参数还真是多,用起来还是很方便的,这么多参数种类可见它在工程上的实用性还是非常广泛的. 挺有意思的,本文主要参照Liaoxuefeng的Python教程. #必选参数 def quadratic ...

  6. 【转】MySQL存储引擎中的MyISAM和InnoDB区别详解

    转自:http://www.jb51.net/article/62457.htm MyISAM是MySQL的默认数据库引擎(5.5版之前),由早期的ISAM(Indexed Sequential Ac ...

  7. c# winform控件dock属性停造位置、摆放顺序详解

    dock : [英文释义- 码头.依靠][winform释义- 获取或设置当前控件依靠到父容器的哪一个边缘.] 用途:多数控件都有这个属性,主要用来设置控件的布局. 但对于不太了解这个属性的朋友来说有 ...

  8. Spring Cloud (12) 服务网关-基础

    通过前几篇介绍,已经可以构建一个简单的微服务架构了,如下图: 通过eureka实现服务注册中心以及服务注册发现,通过ribbon或feign实现服务的消费以及负载均衡,通过spring cloud c ...

  9. Java系列学习(一)-JDK下载与安装

    1.Java语言平台版本 J2SE:Java 2 Platform Standard Edition,java平台标准版 J2ME:Java 2 Platform Micro Edition,java ...

  10. android接收mjpg-streamer软件视频流

    [代码]主要实现代码 package cn.dong.mjpeg; import java.io.InputStream; import java.net.HttpURLConnection; imp ...