题目链接:###

传送门

题目:###

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的更多相关文章

  1. BZOJ3916: [Baltic2014]friends

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 复习一下hash(然后被傻叉错误卡了半天TAT... 取出一个字串:h[r]-h[l-1 ...

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

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

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

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

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

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

  5. 【题解】Bzoj3916

    字符串\(Hash\). 笔者实在太菜了,到现在还没有熟练掌握\(Hash\),就来这里写一篇学习笔记. \(Description\) 有三个好朋友喜欢在一起玩游戏,\(A\)君写下一个字符串\(S ...

  6. BZOJ:(270,300]

    9/30 BZOJ3038:线段树,不带lazy标记,直接修改叶子. BZOJ3211:同3038 BZOJ1406:将式子转换成[(x-1)*(x+1)%n==0]然后枚举i.当i=x-1时,i*( ...

  7. 【hash】Three friends

    [来源]:bzoj3916 [参考博客] BZOJ3916: [Baltic2014]friends [ 哈希和哈希表]Three Friends [Baltic2014][BZOJ3916]frie ...

  8. 【题解】[BalticOI 2014]friends

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 (BZOJ3916) 由题意可知 \(N\) 得为奇数,\(S\) 才存在,所以先特 ...

随机推荐

  1. linux块设备驱动(一)——块设备概念介绍

    本文来源于: 1. http://blog.csdn.net/jianchi88/article/details/7212370 2. http://blog.chinaunix.net/uid-27 ...

  2. android studio Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"

    android studio运行会遇到Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Reques ...

  3. 获取Android系统应用信息

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  4. rails使用mysql数据库

    简单步骤 1,安装mysql 安裝 MySQL Ubuntu 上安裝 MySQL 請執行: $ sudo apt-get install mysql-server mysql-common mysql ...

  5. 关于UISearchBar

    iPhone开发之UISearchBar学习是本文要学习的内容,主要介绍了UISearchBar的使用,不多说,我们先来看详细内容.关于UISearchBar的一些问题. 1.修改UISearchBa ...

  6. usaco2008 nov 区间异或求和

    Problem 11: Switching Lights [LongFan, 2008] Farmer John tries to keep the cows sharp by letting the ...

  7. 根据用户时区显示当地时间 javascript+php

    在跨时区应用中会用到下面代码,这是以前写的一段代码. 服务器保存相关时间配置,保存形式为GMT时间,客户端需要根据客户所在时区做相应显示,以符合客户习惯. ​1. [代码][JavaScript]代码 ...

  8. codeforces B. Online Meeting 解题报告

    题目链接:http://codeforces.com/problemset/problem/420/B 题目意思:给出一段连续的消息记录:记录着哪些人上线或者下线.问通过给出的序列,找出可能为lead ...

  9. codeforces 437A. The Child and Homework 解题报告

    题目链接:http://codeforces.com/problemset/problem/437/A 题目意思:给出四个选项A.B.C.D选项的内容描述,要求选出符合以下条件的一项. (1)如果某个 ...

  10. html5--6-5 CSS选择器2

    html5--6-5 CSS选择器2 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓选择器就是样式作 ...