【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 ...
随机推荐
- maxwell的数据引导方式
INSERT INTO maxwell.bootstrap (database_name, table_name,where_clause) VALUES (--''); INSERT INTO ma ...
- 丰桥运单打印windows/linux环境安装(原)
Linux ①linux下安装jdk1.8,执行命令:yum -y install java ②创建文件夹sf-service将csim_waybill_print_service_V1.0.3.ja ...
- Java基础_死锁、线程组、定时器Timer
一.死锁问题: 死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 比如,线程一需要第一把所,此时锁处于空闲状态,给了 ...
- The JAVA_HOME environment variable is not defined correctly的错误
The JAVA_HOME environment variable is not defined correctlyThis environment variable is needed to ru ...
- js 继承的一个例子
<script type="text/javascript"> function Animal(){ this.species = "动物"; th ...
- 预处理、const、static与sizeof-用#define实现宏并求最大值和最小值
1:实现代码: #define MAX(x,y) (((x)>(y)) ? (x):(y)) #define MIN(x,y) (((x)>(y)) ? (x):(y)) 需要注意的几点: ...
- redis常见7种使用场景
一,简单字符串缓存实例 $redis->connect('127.0.0.1', 6379); $strCacheKey = 'Test_bihu'; //SET 应用 $arrCacheDat ...
- Phos 技术服务支持
Phos Mail: tencenter@163.com
- koa 项目实战(六)注册接口加密
1.创建工具类 根目录/config/tools.js const bcrypt = require('bcryptjs'); const tools = { enbcrypt(password) { ...
- linux内核中有哪些子系统(框架)呢?
注意: 分析用的linux内核版本为5.1.3 1. RTC子系统 2. Remote Processor子系统 3. Remote Processor Message子系统 4. SCSI子系统 5 ...