题意:

有一个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的每一位是否相等

时间复杂度未知,但是实际运行效果,时空复杂度都是超赞的!

(整理了一下代码似乎精简过度影响了可读性了...)

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int n;
  5.  
  6. char s[];
  7.  
  8. int i, j, k, p, l, r;
  9.  
  10. int print() {
  11. int i = ;
  12. for (; i <= l; i ++)
  13. putchar(s[i]);
  14. putchar('+');
  15. for (; i <= r; i ++)
  16. putchar(s[i]);
  17. putchar('=');
  18. for (; i <= n; i ++)
  19. putchar(s[i]);
  20. return ;
  21. }
  22.  
  23. int check(int s1, int s2) {
  24. for (int i = s1, j = s2, k = n, t = ; k > s2; k --) {
  25. if ((s[i] + s[j] + t - ) % != s[k] - )
  26. return ;
  27. t = (s[i] + s[j] + t - ) / ;
  28. i --, j --;
  29. if (i <= ) i = ;
  30. if (j <= s1) j = ;
  31. }
  32. return ;
  33. }
  34.  
  35. int judge() {
  36. if (l + != r && s[l + ] == '') return ;
  37. if (p) {if (s[r + ] != k) return ;}
  38. else if (i != j && (k != s[r + ] && k + != s[r + ])) return ;
  39. if (!check(l, r)) return ;
  40. return print();
  41. }
  42.  
  43. int main() {
  44. gets(s + );
  45. n = strlen(s + );
  46. s[] = '';
  47.  
  48. l = , r = n - n / ;
  49. if (s[] == '') {
  50. print();
  51. return ;
  52. }
  53.  
  54. for (i = + (n & ? : ), j = (n - i) / , r = n - j; i <= n / ; i += , j --, r ++) {
  55. l = j, k = s[];
  56. if (judge()) return ;
  57. l = i, k = s[l + ];
  58. if (judge()) return ;
  59. }
  60.  
  61. p = , k = ;
  62. for (i = + (n & ? : ), j = (n - i) / , r = n - j - ; i <= n / - (n % == ); i += , j --, r ++) {
  63. l = j;
  64. if (judge()) return ;
  65. l = i;
  66. if (judge()) return ;
  67. }
  68. }

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. P5180 【模板】支配树

    这个题乱七八糟的,和之前的灭绝树有点像,但是不一样.那个是DAG,这个是有向图.简单步骤就是先求出来dfs序,然后求出半支配点(?),然后通过这个求支配点. 算法不是很理解,先放在这. 题干: 题目背 ...

  2. bzoj 2333 [SCOI2011]棘手的操作 —— 可并堆

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2333 稍微复杂,参考了博客:http://hzwer.com/5780.html 用 set ...

  3. poj3421 X-factor Chains——分解质因数

    题目:http://poj.org/problem?id=3421 好久没有独立A题了...做点水题还是有助于提升自信心的: 这题就是把 x 质因数分解,质因数指数的和 sum 就是最长的长度,因为每 ...

  4. Angular.forEach用法总结

    }; }]; var so=[]; var so2=[]; var so3=[]; var so4=[]; var so5=[]; var so6=[]; ; ; angular.forEach(so ...

  5. Linex系统 配置php服务器

    此文是可以参考 楼主也不是系统管理员只是迫不得已所以自己才找的  大家可以参考 .... ..... 安装apache 安装mysql 安装PHP 测试服务器 php -v 查询php的版本 就这些了 ...

  6. [Swift通天遁地]三、手势与图表-(9)制作五彩缤纷的气泡图表

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. JavaScript--关闭窗口(window.close)

    close()关闭窗口 用法: window.close(); //关闭本窗口 或 <窗口对象>.close(); //关闭指定的窗口 例如:关闭新建的窗口. <script typ ...

  8. wxwidgets安装环境配置

    一:安装VS2012 wxWidgets-2.9.5( 2.95版本为最稳定版本) 二:打开wxWidgets-2.9.5的安装目录,找到build-msw-wx_vc10.sln打开(等待) 三:打 ...

  9. what is success?

    什么是成功?这是个老话题了.然而每个人对成功的认识却也不同.记得央视主持人王志说过成功是相对的,每个人都有自己的成功标准.有的人认为有钱.有房.有车.有女人,就是成功.有的人则认为成功是你做了一件你想 ...

  10. git的使用注意事项

    这里仅记录了下自己在初次使用git来管理项目的时候遇到的一些注意事项,记录下来备忘以下,以免下次又在这里花太多时间. 1. centos下面已经支持yum install git.来安装. 2. 安装 ...