BZOJ 3916 [Baltic2014]friends Hash
2015-09-30 20:14
423 查看
题意:
《真·每一年的题只会一题跑系列》
有一个串,把它自己接到了自己后面,然后加了一个字符。
给出加完字符后的串,询问原串是啥,如果多解输出一坨,无解输出一坨,有唯一解输出串。
解析:
sb题。
枚举哪个字符是加的即可。
用上hash之后判断就是o(1)辣
细节什么的自己研究吧。
代码:
《真·每一年的题只会一题跑系列》
有一个串,把它自己接到了自己后面,然后加了一个字符。
给出加完字符后的串,询问原串是啥,如果多解输出一坨,无解输出一坨,有唯一解输出串。
解析:
sb题。
枚举哪个字符是加的即可。
用上hash之后判断就是o(1)辣
细节什么的自己研究吧。
代码:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define base 13131 #define N 2000100 using namespace std; typedef unsigned long long ull; ull hash ,pow ; int n; char s ; ull get_hash(int l,int r) { if(l>r)return 0; return hash[r]-hash[l-1]*pow[r-l+1]; } int main() { scanf("%d",&n); scanf("%s",s+1); pow[0]=1; for(int i=1;i<=n;i++) hash[i]=hash[i-1]*base+s[i],pow[i]=pow[i-1]*base; if(!(n&1)){puts("NOT POSSIBLE");return 0;} int tmplen=(n-1)>>1; int tmpl,tmpr; int cnt=0; for(int i=1;i<=n;i++) { int l1=i-1; int l2=n-i; if(l1<l2) { ull hasha=get_hash(1,l1); hasha=hasha*pow[tmplen-l1]+get_hash(i+1,i+tmplen-l1); ull hashb=get_hash(n-tmplen+1,n); if(hasha==hashb) { int tmpll=n-tmplen+1,tmprr=n; if(!cnt) tmpl=tmpll,tmpr=tmprr,cnt++; else if(get_hash(tmpl,tmpr)!=get_hash(tmpll,tmprr)) {cnt++;break;} } }else if(l1==l2) { ull hasha=get_hash(1,tmplen); ull hashb=get_hash(n-tmplen+1,n); if(hasha==hashb) { int tmpll=1,tmprr=tmplen; if(!cnt) tmpl=tmpll,tmpr=tmprr,cnt++; else if(get_hash(tmpl,tmpr)!=get_hash(tmpll,tmprr)) {cnt++;break;} } }else { ull hasha=get_hash(1,tmplen); ull hashb=get_hash(tmplen+1,l1); hashb=hashb*pow[tmplen-(l1-tmplen)]+get_hash(n-l2+1,n); if(hasha==hashb) { int tmpll=1,tmprr=tmplen; if(!cnt) tmpl=tmpll,tmpr=tmprr,cnt++; else if(get_hash(tmpl,tmpr)!=get_hash(tmpll,tmprr)) {cnt++;break;} } } } if(cnt>1)puts("NOT UNIQUE"); else if(cnt==1) { for(int i=tmpl;i<=tmpr;i++)printf("%c",s[i]); puts(""); }else puts("NOT POSSIBLE"); }
相关文章推荐
- maven 插件整理一
- dwr引发的血案之四 内存溢出
- Project Euler 75:Singular integer right triangles
- eclipse 去除svn标识
- Java面试问题集锦04-java程序的基本结构(2)
- 数据结构 PAT 02-线性结构3 Pop Sequence
- 剑指offer—数值的整数次方
- BZOJ 3939 [Usaco2015 Feb]Cow Hopscotch 线段树
- 安装centos 5.3 想用中文出现乱码
- 应用程序通过对键中的气温进行排序来找出最高气温
- 远程推送
- 原 ExpandableListView的用法
- Socket.IO学习之基础入门
- 汇编笔记 CALL(1)
- ocp-9
- ocp-8
- ANR异常产生的原因及其解决办法
- 【错误处理】mysql远程连接ERROR 2003 (HY000): Can't connect to MySQL server on '10.211.55.6' (113)
- 找出数组中第K大的数
- URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)