F. Restoring the Expression
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A correct expression of the form a+b=c was written; ab and c are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that:

  • character'+' is placed on the left of character '=',
  • characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle part — b and the right part — c),
  • all the three parts a, b and c do not contain leading zeros,
  • it is true that a+b=c.

It is guaranteed that in given tests answer always exists.

Input

The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106.

Output

Output the restored expression. If there are several solutions, you can print any of them.

Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be.

Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples.

If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data.

Examples
input
12345168
output
123+45=168
input
099
output
0+9=9
input
199100
output
1+99=100
input
123123123456456456579579579
output
123123123+456456456=579579579

大意:其实看样例就行了

题解:A+B=C A是x位数,B是y位数,C是z位数, x 和 y 中至少有一个和 z 相等或比 z 小 1。
有了这个条件,就可以O(L)枚举+的位置,然后O(1)找出=可能在的位置,关键就是怎么检验值相等? RK hash给了我启示,长度为len的序列的前缀1……m的hash值可以用一个MOD进制数表示,想要知道x……y的hash值:
Hash[x,y]=Hash[1,y]-Hash[1,x-1]*10^(y-x+1)
多取几个MOD来检验就基本可以保证正确性。
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=0,ff=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
return xx*ff;
}
const int maxn=1000010;
char s[maxn];
int len;
int MOD[5]={0,1000000007,19797571,73715923,92431371};
//int MOD[5]={0,1000000,1000000,1000000,1000000};
int Hash[5][maxn];
int mypow(int a,int p,int mod){
int re=1;
while(p){
if(p&1)
re=1LL*re*a%mod;
p>>=1;
a=1LL*a*a%mod;
}
return re;
}
int get_Hash(int xx,int yy,int mod){
//printf("%d %d %d %d %d\n",xx,yy,Hash[mod][yy],Hash[mod][xx-1],mypow(10,yy-xx+1,MOD[mod]));
return ((Hash[mod][yy]-1LL*Hash[mod][xx-1]*mypow(10,yy-xx+1,MOD[mod]))%MOD[mod]+MOD[mod])%MOD[mod];
}
bool check(int x,int y){
if(s[1]=='0'&&x!=1)
return 0;
if(s[x+1]=='0'&&y!=x+1)
return 0;
if(s[y+1]=='0'&&len!=y+1)
return 0;
for(int i=1;i<=4;i++)
if((1LL*get_Hash(1,x,i)+get_Hash(x+1,y,i))%MOD[i]!=get_Hash(y+1,len,i))
return 0;
return 1;
}
int main(){
//freopen("in","r",stdin);
//freopen("out","w",stdout);
gets(s+1);
len=strlen(s+1);
for(int i=1;i<=4;i++)
for(int j=1;j<=len;j++)
Hash[i][j]=(1LL*Hash[i][j-1]*10+s[j]-'0')%MOD[i];
/*for(int i=1;i<=4;i++){
for(int j=0;j<=len;j++)
printf("%d ",Hash[i][j]);
puts("");
}*/
int i,j;
for(i=1;i<=len/2-(len%2==0);i++){
j=len-i;
if(j/2+(j%2==1)>i){
j=i+(j/2);
if(check(i,j))
break;
}
else{
j=len-i;
if(i==j)
break;
if(check(i,j))
break;
j=len-i-1;
if(check(i,j))
break;
}
}
for(int k=1;k<=len;k++){
printf("%c",s[k]);
if(k==i)
printf("+");
else if(k==j)
printf("=");
}
puts("");
return 0;
}

  

细节很多很多,交了五次。

要注意前缀 0 的问题


codeforces 898F Hash的更多相关文章

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

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

  2. F - Restoring the Expression CodeForces - 898F

    字符串hash:  base设置为10 枚举'='可能出现的位置,从1/2处开始到大概1/3处结束,当然大概的1/3不用计算,直接到最后就行,因为本题必然有解,输出直接结束即可. 根据'='号位置,' ...

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

  4. Codeforces Round #109 (Div. 2) E. Double Profiles hash

    题目链接: http://codeforces.com/problemset/problem/155/E E. Double Profiles time limit per test 3 second ...

  5. Codeforces Beta Round #4 (Div. 2 Only) C. Registration system hash

    C. Registration system Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  6. Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash

    E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...

  7. CodeForces 1056E - Check Transcription - [字符串hash]

    题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...

  8. Codeforces Beta Round #7 D. Palindrome Degree hash

    D. Palindrome Degree 题目连接: http://www.codeforces.com/contest/7/problem/D Description String s of len ...

  9. Codeforces 25E Test 【Hash】

    Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...

随机推荐

  1. vim之tags

    好长时间没有上来更新了, 今天趁老板不再上来休闲一下. 本章要说的是和vim的tags相关的内容. 之所以在跳转之后就说明tags是因为这个功能相当的重要和实用. 好的东西自然是需要提前分享的. 首先 ...

  2. (转) Hibernate注解开发

    http://blog.csdn.net/yerenyuan_pku/article/details/70162268 Hibernate注解开发 在Hibernate中我们一般都会使用注解,这样可以 ...

  3. 【剑指Offer】65、矩阵中的路径

      题目描述:   请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经 ...

  4. pandas.DataFrame.quantile

    pandas.DataFrame.quantile 用于返回数据中的 处于1/5    1/2(中位数)等数据

  5. 【数值计算方法】二分法求根的C++简单实现

    给定精确度ξ,用二分法求函数f(x)零点近似值的步骤如下: 1 确定区间[a,b],验证f(a)·f(b)<0,给定精确度ξ. 2 求区间(a,b)的中点c. 3 计算f(c). (1) 若f( ...

  6. python--(十步代码学会线程)

    python--(十步代码学会线程) 一.线程的创建 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getname(): 返回线程名. # setName(): 设置 ...

  7. 6)STM32使用HAL库实现modbus的简单通讯

    1.判断地址.校验 2.读取本机数据并校验打包 3.发送数据包 4.本机数据长度比要读取的长度短怎么办 4.校验错误怎么办

  8. nlogn求LIS(树状数组)

    之前一直是用二分 但是因为比较难理解,写的时候也容易忘记怎么写. 今天比赛讲评的时候讲了一种用树状数组求LIS的方法 (1)好理解,自然也好写(但代码量比二分的大) (2)扩展性强.这个解法顺带求出以 ...

  9. webstorm+nodejs环境中安装淘宝镜像

    用过nodejs的人都知道,从node的官方模板库下载依赖包的时候,经常会遇到“假死”(页面静止不动)的状态,这种速度简直要逼死焦急地等待下班的人.还好咱们万能的淘宝提供了淘宝镜像这么一个不要更好用的 ...

  10. 【微软2017年预科生计划在线编程笔试 A】Legendary Items

    [题目链接]:https://hihocoder.com/problemset/problem/1489 [题意] 每轮游戏; 你一开始有p的概率获得超神标记; 如果这轮游戏你没获得超神标记; 那么你 ...