题目链接:###

传送门

题目:###

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. iOS清理WebView的缓存

    NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; ...

  2. shell mv

    mv $a"/"$b"/"* $a"/"$b"/preview" 移动某个文件夹下的所有文件 使用* 但*不用双引号

  3. miller_rabin模板

    miller_rabin素数测试法 #include <iostream> #include <cstdlib> #include <stdio.h> #inclu ...

  4. POJ2752 Seek the Name, Seek the Fame —— KMP next数组

    题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Li ...

  5. HDU 5178 pairs —— 思维 + 二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5178 pairs Time Limit: 2000/1000 MS (Java/Others)     ...

  6. 计算机学院大学生程序设计竞赛(2015’12)01 Matrix

    01 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 多态、抽象类、接口、区别(java基础知识九)

    1.多态的概述以及代码体现 * A:多态概述 * 事物存在的多种形态 * B:多态前提 * a:要有继承关系. * 一个类是父类,一个类是子类 * b:要有方法重写. * c:要有父类引用指向子类对象 ...

  8. poj 1469 COURSES 解题报告

    题目链接:http://poj.org/problem?id=1469 题目意思:有 N 个人,P个课程,每一个课程有一些学生参加(0个.1个或多个参加).问 能否使得 P 个课程 恰好与 P 个学生 ...

  9. hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)

    题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...

  10. codeforces 696B B. Puzzles(树形dp+概率)

    题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...