题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916

复习一下hash(然后被傻叉错误卡了半天TAT。。。

取出一个字串:h[r]-h[l-1]*power[r-l+1]  然后匹配。。。

注意一下当前需要的是s[i]还是s[i-1],做hash数组时不要写s[i]-'A',写s[i],否则容易被卡

对于这道题,枚举一下断点,注意判重。

对于这种有断点的前面一段+后面一段等于完整一段,要注意前面那一段乘的权。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 2000009
#define p 23333
using namespace std;
typedef unsigned long long ll;
char ans[maxn],s[maxn];
ll h[maxn],bin[maxn],last;
int now,n,mid,anspos;
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)){
if (ch=='-') f=-; ch=getchar();
}
while (isdigit(ch)){
x=x*+ch-''; ch=getchar();
}
return x*f;
}
ll get(int l,int r){
return h[r]-h[l-]*bin[r-l+];
}
int judge(int pos){
ll x,y,z;
int flag=;
if (pos<mid){
x=get(,pos-)*bin[mid-pos]+get(pos+,mid);
y=get(mid+,n);
}
else if (pos>mid){
x=get(,mid-);
y=get(mid,pos-)*bin[n-pos]+get(pos+,n);
}
else if (pos==mid){
x=get(,mid-);
y=get(mid+,n);
}
if (x==y){
if (x==last) return ;
last=x;
int top=;
if (pos<=mid) rep(i,mid+,n) ans[++top]=s[i];
else rep(i,,mid-) ans[++top]=s[i];
return ;
}
return ;
}
int main(){
n=read(); if (n%==) {puts("NOT POSSIBLE"); return ;}
scanf("%s",s+);
bin[]=; rep(i,,n) bin[i]=bin[i-]*p;
rep(i,,n) h[i]=h[i-]*p+s[i];
mid=(n/)+;
int cnt=;
rep(i,,n) {
cnt+=judge(i);
if (cnt>) break;
}
if (!cnt) puts("NOT POSSIBLE");
else if (cnt>) puts("NOT UNIQUE");
else puts(ans+);
return ;
}

BZOJ3916: [Baltic2014]friends的更多相关文章

  1. 【题解】 bzoj3916: [Baltic2014]friends (字符串Hash)

    题面戳我 Solution 首先长度为偶数可以直接判掉 然后我们可以枚举删的位置,通过预处理的\(hash\),判断剩余部分是否划分成两个一样的 判重要注意,我们把字符串分为三个部分\(L_l+1+L ...

  2. 【字符串哈希】bzoj3916 [Baltic2014]friends

    枚举断点,哈希判断. #include<cstdio> using namespace std; typedef unsigned long long ull; ull hs,hs1,hs ...

  3. 【bzoj3916】[Baltic2014]friends 字符串hash

    题目描述 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.现在你得到了U,请你找出S. 输入 第一行一个数N,表示U的长度 ...

  4. BZOJ 3916: [Baltic2014]friends( hash )

    字符串哈希..然后枚举每一位+各种判断就行了 ----------------------------------------------------------------------------- ...

  5. BZOJ_3916_[Baltic2014]friends_hash

    BZOJ_3916_[Baltic2014]friends_hash 题意: 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到 ...

  6. 【BZOJ】3916: [Baltic2014]friends

    http://www.lydsy.com/JudgeOnline/problem.php?id=3916 #include <bits/stdc++.h> using namespace ...

  7. BZOJ3919 : [Baltic2014]portals

    预处理出每个点上下左右能延伸到的最远点以及到它们的距离的最小值md. 然后spfa,一个点除了可以以1的代价到达四周的点之外,还可以以md+1的代价到达四个方向能到达的最远点. #include< ...

  8. bzoj3917: [Baltic2014]sequence

    Description  序列A由从N开始的连续K个数按顺序构成,现在将A中的每个数只保留某一个数码,记为序列B,给定K和B,求可能的最小的N Input 第一行一个数K,第二行K个数B_i Outp ...

  9. BZOJ3916: [Baltic2014]friends

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 题解:随便hash.刚开始看错题WA了N发.(我连双hash都写了!) 代码: #inc ...

随机推荐

  1. 查看windows、linux的SN

     gwmi win32_bios    [root@live-al-ops-pxe-2 ~]# dmidecode | grep Number | sed -n '1p' Serial Number: ...

  2. Ionic2 cordova angular2 打包到Android apk环境搭建

    一.前言 前段时间,公司有个APP项目需要支持不同平台,于是采用了Ionic2 + cordova + angular2,在搭建环境过程中遇到了不少问题,刚好最近有时间整理出来. 二.开发环境搭建 参 ...

  3. 为什么CPU需要时钟这种概念?

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/132 最近在研究计算机里的基本逻辑电路,想到一个问题:为什么CP ...

  4. <!--[if lte IE 8][endif] ]-->IE下判断IE版本的语句

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![endif]--> ...

  5. 写给小白的JAVA链接MySQL数据库的步骤(JDBC):

    作为复习总结的笔记,我罗列了几个jdbc步骤,后边举个简单的例子,其中的try块请读者自行处理. /* * 1.下载驱动包:com.mysql.jdbc.Driver;网上很多下载资源,自己找度娘,此 ...

  6. 文件上传之伪Ajax方式上传

    From: <由 Windows Internet Explorer 8 保存> Subject: =?gb2312?B?zsS8/snPtKvWrs6xQWpheLe9yr3Jz7SrI ...

  7. jQuery模块化开发

    //定义了命名空间. var Itcast = {}; //定义第二级别的 命名空间. var Itcast.Model = {}; var Itcast.Model.UIJs = (function ...

  8. Another option for file sharing(转)

    原文地址  https://security.googleblog.com/2017/02/another-option-for-file-sharing.html Another option fo ...

  9. Java Web高级编程(三)

    使用过滤器改进应用程序 一.过滤器的目的 过滤器是可以拦截访问资源的请求.资源的响应或者同时拦截两者的应用组件.过滤器可以检测和修改请求和响应,同时也可以拒绝.重定向或转发请求.javax.servl ...

  10. 面试经验And总结

    作为一个实习生,我大二即将读完,因为自己是大专的和本科没法比,没有他们的 知识基础,没有他们的充裕的时间,没有那个本科毕业证,没有学位证书.作为一个大专生我在找工作的时候总是充满了自卑,可自己有事那种 ...