【hash】Three friends
【来源】:bzoj3916
【参考博客】
BZOJ3916: [Baltic2014]friends
【 哈希和哈希表】Three Friends
【Baltic2014】【BZOJ3916】friends
【题解】
首先hash整个串,然后分成三种情况,分别是前半段,中间,后半段,三段的字母试图去掉看能否拼起来。
如果可以,那么还需要考虑是否为唯一的。
唯一的意思是: 串S,如果有选择两个不同的串S也能构成这个原串。
代码还是参考别人写的。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 2e6+;
typedef unsigned long long ull ;
const ull base = ; ull Pow[N],Hash[N];
ull Lstr , Rstr , S;
char str[N],Ans[N];
int n,Mid,pos=-; // NOT POSSIBLE = Odd or Not found a S
// NOT UNIQUE = exist least two S
// Else = The answer S // 切割字符串返回hash值
ull Cut_str( int L ,int R ){
ull ans = Hash[R] - Hash[L-] * Pow[R-L+] ;
return ans ;
} bool check (int i){
//如果未被标记过,则标记 返回true
//如果标记过,同时这个字符串和暂存的串不一样则返回false
if( pos == - || S == Lstr ){
pos = i ;
S = Lstr ;
return true ;
}else{
return false ;
}
}
int main()
{
scanf("%d%s",&n,str+);
Mid = n >> ; //偶数情况下无法构成
if( n%== ) return puts("NOT POSSIBLE"),; Pow[] = ;
for(int i=;i<=n;i++){
Pow[i] = Pow[i-] * base ;
Hash[i] = Hash[i-] * base + (ull)(str[i]-'A'+);
//所有字符串只包含大写英文字母
} // delete one character in [1,Mid]
for(int i= ; i<=Mid ; i++ ){
Lstr = Cut_str(,i-) * Pow[Mid+-i] + Cut_str(i+,Mid+);
Rstr = Cut_str(Mid+,n);
if( Lstr == Rstr ) if( !check(i) ) return puts("NOT UNIQUE"),;
} // delete Mid + 1
Lstr = Hash[Mid] ;
Rstr = Hash[n] - Hash[Mid+] * Pow[Mid];
if( Lstr == Rstr ) if( !check(Mid+) ) return puts("NOT UNIQUE"),; // delete [Mid+2,n] for(int i=Mid+ ; i<=n; i++ ){
Lstr = Hash[Mid];
Rstr = Cut_str(Mid+,i-) * Pow[n-i] + Cut_str(i+,n);
if( Lstr == Rstr ) if( !check(i) ) return puts("NOT UNIQUE"),;
} if( pos == - ){
return puts("NOT POSSIBLE"),;
} int len = n/,cnt = ;
for(int i=; i<=n && len ; i++ ){
if( pos == i ) continue;
Ans[cnt++] = str[i] ;
len -- ;
}
Ans[cnt] = '\0';
printf("%s\n",Ans);
return ;
} /* 7
ABXCABC */
【hash】Three friends的更多相关文章
- 【hash】BZOJ3751-[NOIP2014]解方程
[题目大意] 已知多项式方程:a0+a1*x+a2*x^2+...+an*x^n=0.求这个方程在[1,m]内的整数解(n和m均为正整数). [思路] *当年考场上怒打300+行高精度,然而没骗到多少 ...
- 【hash】Power Strings
[题意]: 给出s串出来,能否找到一个前缀 ,通过多次前缀进行拼接.构成s串.如果有多个,请输出最多次数那个. 如:aaaa 可以用1个a,进行4次拼接 可以用2个a,进行2次拼接 可以用4个a,进行 ...
- 【hash】Similarity of Subtrees
图片来源: https://blog.csdn.net/dylan_frank/article/details/78177368 [题意]: 对于每一个节点来说有多少对相同的子树. [题解]: 利用层 ...
- 【hash】Seek the Name, Seek the Fame
[哈希和哈希表]Seek the Name, Seek the Fame 题目描述 The little cat is so famous, that many couples tramp over ...
- 【hash】A Horrible Poem
[题目链接] # 10038. 「一本通 2.1 练习 4」A Horrible Poem [参考博客] A Horrible Poem (字符串hash+数论) [题目描述] 给出一个由小写英文字母 ...
- 湖南师范大学2018年大学生程序设计竞赛新生赛 A 齐神和心美的游戏【hash】
[链接]:A [题意]:给你n个数的序列和k.判断是否可以三个数组成k(同一个数可以拿多次) [分析]:每个数vis记录一下.2层循环.两数之和不超过k以及剩下的数出现在序列中那么ok. [代码]: ...
- 【hash】珍珠
[来源] https://loj.ac/problem/2427 [参考博客] LOJ#2427. 「POI2010」珍珠项链 Beads [题解]: 复杂度计算: 暴力枚举k每次计算是n/2+n/3 ...
- 洛谷P1117 优秀的拆分【Hash】【字符串】【二分】【好难不会】
题目描述 如果一个字符串可以被拆分为AABBAABB的形式,其中 A和 B是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串aabaabaaaabaabaa,如果令 A=aabA ...
- hdu 5183 Negative and Positive (NP)(STL-集合【HASH】)
题意: When given an array (a0,a1,a2,⋯an−1) and an integer K, you are expected to judge whether there i ...
随机推荐
- div设置百分比高度 宽度
给div按百分比设置高度 宽度两种方法: 第一种是给body标签设置他的高度值,xxxpx,div就会根据body的像素值取百分比: 第二种方法就是在div属性中加入 position:absolut ...
- Process.Net
ProcessSharp的构造函数,对应的测试是 https://github.com/lolp1/Process.NET/blob/master/test/Process.NET.Test/Core ...
- kotlin class
每个次构造函数需要委托给主构造函数, 可以直接委托或者通过别的次构造函数间接委托.委托到同一个类的另一个构造函数用 this 关键字即可 class Person { constructor(pare ...
- 中间件 | kafka简介、使用场景、设计原理、主要配置及集群搭建
开源Java学习 公众号 一.入门 1.简介 Kafka is a distributed,partitioned,replicated commit logservice.它提供了类似于JMS的特性 ...
- 前端知识点回顾——Nodejs
Node.js 事件循环机制 setImmediate()是将事件插入到事件队列尾部,主线程和事件队列的函数执行完成之后立即执行setImmediate指定的回调函数,和setTimeout(fn,0 ...
- 修改vscode终端样式
在设置中查找workbench,然后编辑setting.json: "terminal.integrated.cursorBlinking": true, "termin ...
- Docker的镜像制作与整套项目一键打包部署
Dockerfile常用指令介绍 指令 描述 FROM 构建的新镜像是基于哪个镜像.例如:FROM centos:6 MAINTAINER 镜像维护者姓名或邮箱地址.例如:MAINTAINER Mr. ...
- input上传指定类型的文件
1. 谷歌–上传文件夹 添加属性webkitdirectory <input type = "file" webkitdirectory> 2. 上传文件–限制类型 添 ...
- easyUI之window窗口
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- ansible简单入门
1,结构框架 Ansible 使用的是无代理体系结构,这种体系结构可以通过防止节点轮询控制机器来减少网络开销.Ansible 提供的结果框架如下所示: Ansible :运行在中央计算机上: Conn ...