Codeforces 898F - Restoring the Expression(字符串hash)
898F - Restoring the Expression
思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+;
const int base=;
const int MOD=1e9+;
ll h[N];
ll p[N];
int num[N];
string s;
void init(string s)
{
int l=s.size();
h[]=;
for(int i=;i<l;i++)
{
h[i+]=(h[i]*base+s[i]-'')%MOD;
}
p[]=;
for(int i=;i<=l;i++)p[i]=(p[i-]*base)%MOD;
}
ll get(int l,int r)
{
if(r-l+<=||l-<||r<)return ;
return ((h[r]-h[l-]*p[r-l+])%MOD+MOD)%MOD;
}
bool is(int l,int r)
{
if(l<||r>=s.size()-||l>=r)return false;
if((r-l>=&&s[l+]=='')||(s.size()>=r+&&s[r+]==''))return false;
mem(num,);
string s1=s.substr(,l+);
string s2=s.substr(l+,r-l);
string s3=s.substr(r+,s.size()-r-);
//cout<<s1<<' '<<s2<<' '<<s3<<endl;
if(s2.size()>s3.size()||s1.size()>s3.size())return false;
if(s1.size()<s2.size())swap(s1,s2);
int tot=;
for(int i=s3.size()-;i>=;i--)
{
if(tot<=s2.size())
{
int t=s1[s1.size()-tot]-''+s2[s2.size()-tot]-''+num[i];
if(t>=)
{
if(i==)return false;
num[i-]=t/;
num[i]=t=t%;
}
if(t!=s3[i]-'')return false;
}
else if(tot<=s1.size())
{
int t=s1[s1.size()-tot]-''+num[i];
if(t>=)
{
if(i==)return false;
num[i-]=t/;
num[i]=t=t%;
}
if(t!=s3[i]-'')return false;
}
else
{
if(num[i]>=)
{
if(i==)return false;
num[i-]=num[i]/;
num[i]=num[i]%;
}
if(num[i]!=s3[i]-'')return false;
}
tot++;
}
return true;
}
void solve(string s)
{
init(s);
int pos=,_pos=;
int l=s.size();
for(int i=l-;i>=;i--)
{
int t=l-i;
ll sumh=get(i+,l);
if((get(,t)+get(t+,i))%MOD==sumh){
pos=t-;
_pos=i-;
if(is(pos,_pos))break;
else pos=_pos=;
}
if((get(,t-)+get(t,i))%MOD==sumh){
pos=t-;
_pos=i-;
if(is(pos,_pos))break;
else pos=_pos=;
}
if((get(,i-t)+get(i-t+,i))%MOD==sumh){
pos=i-t-;
_pos=i-;
if(is(pos,_pos))break;
else pos=_pos=;
}
if((get(,i-t+)+get(i-t+,i))%MOD==sumh){
pos=i-t;
_pos=i-;
if(is(pos,_pos))break;
else pos=_pos=;
}
} for(int i=;i<=pos;i++)putchar(s[i]);
putchar('+');
for(int i=pos+;i<=_pos;i++)putchar(s[i]);
putchar('=');
for(int i=_pos+;i<l;i++)putchar(s[i]);
puts("");
//cout<<pos<<' '<<_pos<<endl;
//cout<<get(1,1)<<' '<<get(2,22)<<' '<<get(23,44)<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>s;
solve(s);
return ;
}
Codeforces 898F - Restoring the Expression(字符串hash)的更多相关文章
- codeforces gym 101164 K Cutting 字符串hash
题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...
- 【题解】 Codeforces Edu41 F. k-substrings (字符串Hash)
题面戳我 Solution 我们正着每次都要枚举从长到短,时间复杂度承受不了,但是我们可以发现一个规律,假设某次的答案为\(x\),那么这个字符串为\(A+X+B\)组成,无论中间的\(X\)是重叠还 ...
- Contest Round #451 (Div. 2)F/Problemset 898F Restoring the Expression
题意: 有一个a+b=c的等式,去掉两个符号,把三个数连在一起得到一个数 给出这个数,要求还原等式,length <= 1e6 三个数不能含有前导0,保证有解 解法: 铁头过题法,分类然后各种判 ...
- F - Restoring the Expression CodeForces - 898F
字符串hash: base设置为10 枚举'='可能出现的位置,从1/2处开始到大概1/3处结束,当然大概的1/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 解 从前往后枚举,放进 ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
- 【codeforces 514C】Watto and Mechanism(字符串hash)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- codeforces 898F Hash
F. Restoring the Expression time limit per test 2 seconds memory limit per test 256 megabytes input ...
- CodeForces - 727E Games on a CD 字符串Hash
题意:有n个单词,每个单词长度为k,顺时针将它们写成一个圆圈串.现在知道g个长度为k的单词,是否可以从这g个单词中选择n个形成这个圆圈串?如果有多个答案,任意输出一个. 思路 可以发现,如果枚举第一个 ...
随机推荐
- sql怎么批量替换字段里的字符串的
方法一:varchar和nvarchar类型是支持replace,所以如果你的text不超过8000可以先转换成前面两种类型再使用replace 替换 text ntext 数据类型字段的语句 . 1 ...
- android实操--练习2
练习2是实现一个计算器的功能:可以加减乘除:可以倒退,可以清空文本. 下面是效果展示: -----------------------布局------------------------------- ...
- Python: 读文件,写文件
读写文件是最常见的IO操作.Python内置了读写文件的函数. 读写文件前,我们先了解一下,在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求 ...
- 安全测试===sqlmap(壹)转载
六.优化 这些参数可以优化Sqlmap的性能. 1.一键优化 参数:-o 添加此参数相当于同时添加下列三个优化参数: --keep-alive --null-connection --threads= ...
- python类方法、静态方法、实例方法例子
类方法,静态方法,普通方法 #coding=utf-8 class Foo: def __init__(self,name): self.name=name d ...
- Python之路----生成器函数进阶
def generator(): print(123) yield 1 print(456) yield 2 g = generator() ret = g.__next__() print('*** ...
- 20145335郝昊《网络对抗》Exp 8 Web基础
20145335郝昊<网络对抗>Exp 8 Web基础 实验内容 本实践的具体要求有: Web前端HTML:能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法, ...
- 用Python为iOS和Android写跨平台的应用
首先保证安装了最新的python(当前安装的是python3.6) 一.安装Kivy :python -m pip install --upgrade pip wheel setuptools pyt ...
- CF873B Balanced Substring
1到n内0,1个数相同的个数的最长字串 \(i>=j\) \[1的个数=0的个数\] \[sum[i]-sum[j-1]=i-(j-1) - (sum[i]-sum[j-1])\] 这里把\(( ...
- 51NOD 1117 聪明的木匠
来源:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 挑战原题吧 大概 每次挑选最小的两个,合起来 #inclu ...