bzoj 3916 friends | 字符串hash
2015-11-16 10:38
399 查看
很裸的hash,我开始读错题了,以为是加入位置不唯一,实质是s串不唯一
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #define md #define ull unsigned long long #define inf (int) 1e9 #define eps 1e-8 #define N 2000010 using namespace std; ull mi ,f ,cg=27,hash; char st ; int ans=0; void solve(int x,ull ha) { if (ans==0||hash==ha) ans=x,hash=ha; else { printf("NOT UNIQUE\n"); exit(0); } } inline ull get(int l,int r) { ull a=f[r]-f[l-1]*mi[r-l+1]; return a; } int main() { int n; scanf("%d",&n); if (!(n&1)) { printf("NOT POSSIBLE\n"); return 0;} mi[0]=1; for (int i=1;i<=n;i++) mi[i]=mi[i-1]*cg; scanf("%s",st+1); for (int i=1;i<=n;i++) f[i]=f[i-1]*cg+(ull)(st[i]-'A'+1); int mid=n>>1; ull a,b; for (int i=1;i<=mid;i++) { a=get(1,i-1)*mi[mid-i+1]+get(i+1,mid+1); b=get(mid+2,n); if (a==b) solve(i,a); } a=f[mid]; b=f -f[mid+1]*mi[mid]; if (a==b) solve(mid+1,a); for (int i=mid+2;i<=n;i++) { a=f[mid]; b=get(mid+1,i-1)*mi[n-i]+get(i+1,n); if (a==b) solve(i,a); } if (!ans) { printf("NOT POSSIBLE\n"); return 0;} if (ans<=mid+1) for (int i=mid+2;i<=n;i++) printf("%c",st[i]); else for (int i=1;i<=mid;i++) printf("%c",st[i]); printf("\n"); return 0; }
相关文章推荐
- bzoj 1047 理想的正方形
- bzoj 3918 postman
- bzoj 3098 hash killer
- bzoj 1875 HH去散步
- bzoj 3679 数字之积
- bzoj 屯题计划 完结
- bzoj 3503 和谐矩阵
- bzoj 2242 计算器
- bzoj 2597 石头剪刀布
- bzoj 3532 lis
- bzoj 3130 费用流
- bzoj 1500 维修数列 bzoj 1507 editor
- bzoj 1251 序列终结者
- bzoj 4152 the camptin (dis[x][y]=min(|x.x-y.x|,|x.y-y.y|),求1,n最短路)
- bzoj 3029 守卫者的挑战
- bzoj 2118 墨墨的等式
- <android5.0>之曲线动画(Curved motion(曲线运动))
- 浅谈虚数i在电路分析中的作用
- 【数据库7】字段相关与联合结果集
- Jenkins入门系列之——02第二章 Jenkins安装与配置