[BZOJ3916/WOJ3815]Friends
题目链接:###
题目:###
Description####
有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.现在你得到了U,请你找出S.
Input####
第一行一个数N,表示U的长度.
第二行一个字符串U,保证U由大写字母组成
Output####
输出一行,若S不存在,输出"NOT POSSIBLE".若S不唯一,输出"NOT UNIQUE".否则输出S.
题目分析:###
枚举断点+字符串前缀和哈希
如果一个字符串中去掉一个字母(或一段),它的哈希值等于前面串的哈希值*base^(len[后面串])+后面。
代码:###
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int cnt=0,f=1;char c;
c=getchar();
while(!isdigit(c)){
if(c=='-')f=-f;
c=getchar();
}
while(isdigit(c)){
cnt=cnt*10+c-'0';
c=getchar();
}
return cnt*f;
}
int n;
const int P=131;
char s[2000005];
unsigned long long Hash[2000005];
unsigned long long bin[2000005];
int flag=0;
unsigned long long hash_left;unsigned long long hash_right;
int pos=-1;
unsigned long long ans=0;
int main(){
n=read();scanf("%s",s+1);
if(n%2==0){
printf("NOT POSSIBLE");
return 0;
}
bin[0]=1;
for(register int i=1;i<=n;i++){
Hash[i]=Hash[i-1]*P+s[i];
bin[i]=bin[i-1]*P;
}
// for(register int i=1;i<=n;i++)printf("%d ",hash[i]);
int mid=(n+1)/2;
for(register int i=1;i<=n;i++){
hash_left=0;hash_right=0;
if(i<mid){
hash_left=Hash[i-1]*bin[mid-i]+Hash[mid]-Hash[i]*bin[mid-i];
hash_right=Hash[n]-Hash[mid]*bin[n-mid];
}
if(i==mid){
hash_left=Hash[i-1];
hash_right=Hash[n]-Hash[mid]*bin[n-mid];
}
if(i>mid){
hash_left=Hash[mid-1];
hash_right=(Hash[i-1]-Hash[mid-1]*bin[i-mid])*bin[n-i]+Hash[n]-Hash[i]*bin[n-i];
}
// cout<<hash_left<<" "<<hash_right<<endl;
if(hash_left==hash_right){
if(flag==0)flag++,pos=i,ans=hash_left;
else
if(ans!=hash_left){
flag++;
break;
}
}
}
if(flag>1)printf("NOT UNIQUE");
if(flag==0)printf("NOT POSSIBLE");
if(flag==1){
if(pos<mid)
for(register int i=mid+1;i<=n;i++)printf("%c",s[i]);
if(pos>mid)
for(register int i=1;i<mid;i++)printf("%c",s[i]);
if(pos==mid)
for(register int i=1;i<mid;i++)printf("%c",s[i]);
}
return 0;
}
[BZOJ3916/WOJ3815]Friends的更多相关文章
- BZOJ3916: [Baltic2014]friends
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 复习一下hash(然后被傻叉错误卡了半天TAT... 取出一个字串:h[r]-h[l-1 ...
- 【题解】 bzoj3916: [Baltic2014]friends (字符串Hash)
题面戳我 Solution 首先长度为偶数可以直接判掉 然后我们可以枚举删的位置,通过预处理的\(hash\),判断剩余部分是否划分成两个一样的 判重要注意,我们把字符串分为三个部分\(L_l+1+L ...
- 【字符串哈希】bzoj3916 [Baltic2014]friends
枚举断点,哈希判断. #include<cstdio> using namespace std; typedef unsigned long long ull; ull hs,hs1,hs ...
- 【bzoj3916】[Baltic2014]friends 字符串hash
题目描述 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.现在你得到了U,请你找出S. 输入 第一行一个数N,表示U的长度 ...
- 【题解】Bzoj3916
字符串\(Hash\). 笔者实在太菜了,到现在还没有熟练掌握\(Hash\),就来这里写一篇学习笔记. \(Description\) 有三个好朋友喜欢在一起玩游戏,\(A\)君写下一个字符串\(S ...
- BZOJ:(270,300]
9/30 BZOJ3038:线段树,不带lazy标记,直接修改叶子. BZOJ3211:同3038 BZOJ1406:将式子转换成[(x-1)*(x+1)%n==0]然后枚举i.当i=x-1时,i*( ...
- 【hash】Three friends
[来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...
- 【题解】[BalticOI 2014]friends
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 (BZOJ3916) 由题意可知 \(N\) 得为奇数,\(S\) 才存在,所以先特 ...
随机推荐
- Android中Looper的quit方法和quitSafely方法
Looper是通过调用loop方法驱动着消息循环的进行: 从MessageQueue中堵塞式地取出一个消息.然后让Handler处理该消息,周而复始.loop方法是个死循环方法. 那怎样终止消息循环呢 ...
- 初识跨终端Web
近期试读了<跨终端Web>这本书的部分章节,既为了拿到书,也为了记录下读后的收获的东西,这会是个非常好的习惯吧. 标题为"初识跨终端Web".对我来说最贴切了,在此之前 ...
- HDU Shell Necklace CDQ分治+FFT
Shell Necklace Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in ...
- (29)java web的hibernate使用-crud的dao
1, 做个简单的util public class HibernateUtils { private static SessionFactory sf; static { //加载主要的配置文件 sf ...
- (24) java web的struts2框架的使用-action参数自动封装与类型转换
structs可以对参数进行自动封装,做法也很简单. 一,action参数自动封装: 1,可以直接在action类中,声明public的属性,接受参数. 2,属性也是是private,如果是priva ...
- linux下如何用php读取word
在实际的工作中遇到到要导入word格式的文件,经过努力,终于成功了. 在linux上用PHP读取WORD文档,其实是使用了 antiword程序把word文档转化为txt文档. 再使用php执行系统命 ...
- 如何在 Ubuntu 云服务器上部署自己的 Rails 应用
安装步骤 参考:https://ruby-china.org/topics/32851 在云服务器上安装Ruby|Rails : http://www.cnblogs.com/znsongshu/p ...
- js 时间戳精确值的问题
最近做一个多图上传的功能,通过name + 时间戳命名,结果发现时间戳竟然一样,一直以为是代码逻辑的问题,结果出错在时间戳的获取上了. 关于时间戳的获取方式: 1.Date.parse() var d ...
- mysql 中varchar(50)最多能存多少个汉字
首先要确定mysql版本4.0版本以下,varchar(50),指的是50字节,如果存放UTF8汉字时,只能存16个(每个汉字3字节) 5.0版本以上,varchar(50),指的是50字符,无论存放 ...
- MAC 开发 stm32 程序
资料:http://www.cnblogs.com/humaoxiao 大神技术.