【 哈希和哈希表】Three Friends【进制哈希】
Three Friends
传送门:链接 (UPC)或 链接(大视野)
题目描述
Three friends like to play the following game. The first friend chooses a string S. Then the second friend constructs a new string T that consists of two copies of the string S. finally, the third friend inserts one letter at the beginning, the end or somewhere inside the string T, thereby creating a string U.
You are given the string U and your task is to reconstruct the original string S.
输入
The first line of the input contains N(2 ≤ N ≤ 2000001), the length of the final string U. The string U itself is given on the second line. It consists of N uppercase English letters (A, B, C, . . . , Z).
输出
Your program should print the original string S. However, there are two exceptions:
1.If the final string U could not have been created using the above procedure, you should print NOT POSSIBLE.
2.If the original string S is not unique, you should print NOT UNIQUE.
样例输入
7
ABXCABC
样例输出
ABC
题目大意:
有三个字符串分别为S,T,U,其中T=S+S,U为在T中插入一个字符。
现在给出字符串U,判断是否存在原始字符串S,或者是存在不止一个。
aaa 应该输出 a ,而不应该判断为不存在。
解题思路:
hash应该能想到,但我T了n遍。
1、数据量很大,最好用scanf或者加ios::sync_with_stdio(false);。
2、暴力每个点,判断这个点的字符去掉后左右两边的hash值是否相等。
3、解决aaa的问题,我开始是用map标记,但因为map要用string做key键,导致用了很多string函数导致超时,解决这个问题其实可以保存第一个符合条件的hash值,以后直接判断。
4、在遍历过程中,一旦有两个hash值不相同的点且都满足条件,立刻输出并return 0也是优化的关键。
AC代码:
我用的hash方法是进制哈希(我没取模肯定会导致爆long long,建议你们取模):详解进制哈希
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX=2e6;
LL k=27;
LL ha[MAX+5];
LL qpow(LL m,LL q)
{
LL ans=1;
while(q){
if(q%2) ans*=m;
m*=m;
q/=2;
}
return ans;
}
void hasha(string a)
{
LL la=a.size();
la-=1;
for(LL i=la;i>=1;i--){
ha[i]=ha[i+1]*k+a[i];
}
}
int main()
{
ios::sync_with_stdio(false);
LL la;
string a;
cin>>la>>a;
a=' '+a;
if(la%2==0){
cout<<"NOT POSSIBLE"<<endl;
return 0;
}
hasha(a);
LL flag=-1;
long long flag1;
for(LL i=1;i<=la;i++){
if(i==(la/2+1)){
if((ha[1]-ha[i]*qpow(k,i-1)==(ha[i+1]))){
if(flag==-1){
flag1=ha[i+1];
flag=i;
}else{
if(ha[i+1]!=flag1){
cout<<"NOT UNIQUE"<<endl;
return 0;
}
}
}
}else if(i<(la/2+1)){
LL x=i-1;
LL y=la/2-i+1;
LL num1=la/2+2;
if(((ha[i+1]-ha[num1]*qpow(k,y))*qpow(k,x)+(ha[1]-ha[i]*qpow(k,x)))==ha[num1]){
if(flag==-1){
flag1=ha[num1];
flag=i;
}else{
if(ha[num1]!=flag1){
cout<<"NOT UNIQUE"<<endl;
return 0;
}
}
}
}else if(i>(la/2+1)){
LL x=i-la/2-1;
LL num1=la/2+1;
if(ha[i+1]*qpow(k,x)+(ha[num1]-ha[i]*qpow(k,x))==(ha[1]-ha[num1]*qpow(k,la/2))){
if(flag==-1){
flag1=ha[1]-ha[num1]*qpow(k,la/2);
flag=i;
}else{
if((ha[1]-ha[num1]*qpow(k,la/2))!=flag1){
cout<<"NOT UNIQUE"<<endl;
return 0;
}
}
}
}
}
if(flag==-1) cout<<"NOT POSSIBLE"<<endl;
else{
LL dir=flag,j=1;
for(LL i=1;;i++){
if(i==dir) continue;
else{
cout<<a[i];
j++;
}
if(j==la/2+1) break;
}
cout<<endl;
}
return 0;
}
【 哈希和哈希表】Three Friends【进制哈希】的更多相关文章
- 数据结构之哈希(hash)表
最近看PHP数组底层结构,用到了哈希表,所以还是老老实实回去看结构,在这里去总结一下. 1.哈希表的定义 这里先说一下哈希(hash)表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过 ...
- C语言:十进制进制转换为其他进制(思想:查表法)
// // main.c // Hex conversion // // Created by ma c on 15/7/22. // Copyright (c) 2015年 bjsxt. A ...
- 为什么分库分表使用2的N次方 一个字节用两位16进制
你说说为神马表的总数.redis库的总数.HashMap的数量最好是2的N次方 数据在表库HashMap 落地时候都会跟总数取模,这个我们做个测试 假设数量是2的3次方就是8,即索引就是0-7 php ...
- Windows 注册表 16进制时间转换( Convert Reg_binary Time to a Datetime )
背景: Windows注册表中,存在大量16进制的时间,以 reg_binary存储在注册表中. 例如: 0D 6C A4 4B 37 C5 CE 01 这种值日常报表中需要转换为适合人阅读的格式,实 ...
- C语言之冒泡排序、选择排序、折半查询、进制查表
菜单导航 1.冒泡排序 2.选择排序 3.折半查询 4.进制查表(十进制转二进制.八进制.十六进制) 一.冒泡排序 //1.冒泡排序 /** 一组无序数字,进行从小到大排序 冒泡排序的过程:就是每个循 ...
- [原创]K8飞刀20160613 Plesk密码 & 注册表16进制转换 & Html实体解密
K8飞刀 by K8拉登哥哥@[K8搞基大队]博客: http://qqhack8.blog.163.com 简介: K8飞刀是一款多功能的安全测试工具. Hacker Swiss Army Kni ...
- ASCII码与16进制的互相转换(表)
所谓的ASCII和16进制都只是概念上的东西,在计算机中通通是二进制 转换应该是输出的转换,同样是一个数,在计算机内存中表示是一样的,只是输出不一样ASCII是针对字符的编码,几乎是键盘上的字符的编码 ...
- 算法初级面试题05——哈希函数/表、生成多个哈希函数、哈希扩容、利用哈希分流找出大文件的重复内容、设计RandomPool结构、布隆过滤器、一致性哈希、并查集、岛问题
今天主要讨论:哈希函数.哈希表.布隆过滤器.一致性哈希.并查集的介绍和应用. 题目一 认识哈希函数和哈希表 1.输入无限大 2.输出有限的S集合 3.输入什么就输出什么 4.会发生哈希碰撞 5.会均匀 ...
- 【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式
boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #includ ...
随机推荐
- scroll 方法 获取滚轴距离顶部高度
$(window).scroll(function(){ var supportPageOffset = window.pageXOffset !== undefined; // 判断是否支持page ...
- excel导入DataTable
http://www.cnblogs.com/top5/archive/2010/03/12/1684559.html --下载excel的dll http://bbs.csdn.net/topics ...
- 【Mac】anaconda自定义channels
1.查看channels conda config --show channels 输出 channels: - defaults 2.添加channels conda config --add ch ...
- eclipse——Error exists in required project Proceed with launch?
运行java文件时报错: Error exists in required project Proceed with launch? 报错截图: 问题参生原因:开始Buildpath了一个jar ...
- web项目——javax.servlet.ServletException: Circular view path [registerForm]
报错: 控制台输出: 三月 21, 2019 10:12:32 上午 org.springframework.web.servlet.PageNotFound noHandlerFound 警告: N ...
- [原创]RTX使用printf输出后进入hardfault中断的处理方法 - 讨论
今天我用到RTX里面使用printf ,发现程序死掉了 我发现很多人遇到了这样的问题 找了网上很多的文章,说是这个是RTX的一个先天不足的问题 我发现了正点原子的 原子哥的解决方案,如下所示: --- ...
- 剑指Offer之跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 解法1:递归解法 public int JumpFloor(int t ...
- [安卓基础] 007.管理Activity的生命周期
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 获取Class实例的方式与类加载器
1. java.lang.Class: 是反射的源头 2.如何获取Class的实例(3种) 3.关于类的加载器 TestReflection2 package com.aff.reflection ...
- Spring ( 四 )Spring的AOP动态代理、切面编程
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.AOP切面编程 1.什么是AOP AOP是面向切面编程.全称:Aspect Oriented Pro ...