bzoj3916: [Baltic2014]friends
来源:互联网 发布:俄罗斯民族性格 知乎 编辑:程序博客网 时间:2024/06/04 19:17
链接
http://www.lydsy.com/JudgeOnline/problem.php?id=3916
题解
枚举删除位置,哈希判断
我查了一上午的错,就因为题没读好。长得一样的串,算一个。
代码
//哈希#include <cstdio>#include <algorithm>#define maxn 3000000#define mod 1000000007#define base 4894651ll#define ll long longusing namespace std;int N, h[maxn], inv[maxn], b[maxn];char s[maxn];void init(){ int i; scanf("%d%s",&N,s+1); for(b[0]=i=1;i<=N;i++)b[i]=(ll)b[i-1]*base%mod; for(inv[0]=1,i=1;i<=N;i++)inv[i]=(inv[i-1]*612582597ll)%mod; for(i=1;i<=N;i++)h[i]=((ll)(s[i]-'A')*b[i-1]+h[i-1])%mod;}inline int geth(int l, int r){ if(l>r)return 0; return ((ll)h[r]-h[l-1])*inv[l-1]%mod;}inline int check(int pos){ int h1, h2; if(pos<=(N>>1)) { h1=(geth(1,pos-1)+(ll)geth(pos+1,(N>>1)+1)*b[pos-1]%mod)%mod; h2=geth((N>>1)+2,N); } else { h1=geth(1,N>>1); h2=(geth((N>>1)+1,pos-1)+(ll)geth(pos+1,N)*b[pos-(N>>1)-1]%mod)%mod; } return (h1+mod)%mod == (h2+mod)%mod ? (h1+mod)%mod : -1;}void show(int pos){ int i; for(i=pos;i<N;i++)s[i]=s[i+1]; for(i=1;i<=(N>>1);i++)putchar(s[i]);}int main(){ int i, pos, cnt=0, H=-1, t; init(); if((~N&1) or N<3){printf("NOT POSSIBLE");return 0;} for(i=1;i<=N;i++) { t=check(i); if(t^-1 and t^H) { pos=i;cnt++;H=t; if(cnt>1)break; } } if(cnt==0){printf("NOT POSSIBLE");return 0;} if(cnt>1){printf("NOT UNIQUE");return 0;} show(pos); return 0;}
0 0
- 【BZOJ3916】【Baltic2014】friends 暴力
- 【bzoj3916】 Baltic2014 friends hash
- 【Baltic2014】【BZOJ3916】friends
- [BZOJ3916] [Baltic2014]friends
- 【bzoj3916】【Baltic2014】【friends】【hash】
- BZOJ3916 [Baltic2014]friends
- bzoj3916: [Baltic2014]friends
- bzoj3916 [Baltic2014]friends 暴力
- bzoj 3916: [Baltic2014]friends
- BZOJ 3916 [Baltic2014]friends Hash
- bzoj 3916: [Baltic2014]friends hash
- friends
- friends.....
- Friends
- Friends
- Friends
- friends
- friends
- 【c/c++】如何调用【linux】shell命令行命令并获取命令行的输出内容
- 时间日期格式转换
- 容器类的同步
- 创新者并不一定是科学家
- 指针与二维指针、引用
- bzoj3916: [Baltic2014]friends
- 第二章预习(2)
- zephyr学习笔记---双向链表dlist
- java中的stack和queue
- css--2.常见属性
- 算法设计与应用基础:第九周(1)
- Java 多线程编程
- MySQL基础知识五
- 动态规划练习——公共子序列