DAY8 HASH


一、HASH是什么?

Hash,一般翻译做散列、杂凑,或音译为哈希,是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来确定唯一的输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。

也就是说是将一些相应的信息(如字符串数组求重复串)通过转化成单条信息来保存和计算,可以减少时间复杂度

二、使用步骤

构造方法

构造一个hash

const int p=131
long long h[N]
h[0]=0;
for (int i = 1; i <= len; ++i)
		h[i] = h[i - 1] * p + s1[i];

取值方法

从第1位到第i位的值就是h[i]

如果我们需要取第l位到第r位的值
即:(1到r)的值减(1到l-1)的值

代码如下:

unsigned long long get_ha(int l, int r)
{
	return (h[r] - h[l - 1] * po[r - l + 1]);
}

这里的po是存预处理完的 p^i 的值
po[i]=pow(p,i);

例题

问题

Question

Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse
it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you
passed it on to your inexperienced team-mate. When the contest is almost over, you find out that
that problem still isn’t solved. The problem with the code is that the strings generated are often not
palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing
that the situation is desperate, you decide to simply write some additional code that takes the output
and adds just enough extra characters to it to make it a palindrome and hope for the best. Your
solution should take as its input a string and produce the smallest palindrome that can be formed by
adding zero or more characters at its end.

Input

Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of
upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than
or equal to 100,000.

Output

For each line of input, output will consist of exactly one line. It should contain the palindrome formed
by adding the fewest number of extra letters to the end of the corresponding input string.

Sample Input

aaaa
abba
amanaplanacanal
xyz

Sample Output

aaaa
abba
amanaplanacanalpanama
xyzyx

方法

判断到最后一位为回文串的最远距离
然后把会问前方的倒序输出

代码

#define _CRT_SECURE_NO_DEPRECATE
#include<bits/stdc++.h>
#define RE0 return 0;
#define pi acos(-1.0)
const long long mod = (1e9 + 1e5 + 1e3 + 7);
const int N=100007;
using namespace std;

unsigned long long h[N], po[N], antih[N], p = 131;
char s1[N + 1];

void start() //预处理
{
	po[0] = 1;
	for (int i = 1; i < N; ++i)
	{
		po[i] = po[i - 1] * p;
	}
}
unsigned long long get_ha(int l, int r) //取l~r
{
	return (h[r] - h[l - 1] * po[r - l + 1]);
}
unsigned long long get_ha2(int l, int r) //倒叙取
{
	return (antih[r] - antih[l - 1] * po[r - l + 1]);
}
int main()
{
	start();
	while (scanf("%s", s1 + 1) != EOF)
	{
		int len = strlen(s1 + 1);
		for (int i = 1; i <= len; ++i)
		{
			h[i] = h[i - 1] * p + s1[i];  //正序
			antih[i] = antih[i - 1] * p + s1[len - i + 1]; //倒叙
		}
		int minlen = 0;
		for (int i = 1; i <= len; ++i)
		{
			unsigned long long pre = get_ha(i, len);
			unsigned long long suf = get_ha2(1, len - i + 1);
			if (pre == suf)  //通过判断正序与倒叙是否相等来判断是否回文
			{
				minlen = i - 1;
				break;
			}
		}
		printf("%s", s1 + 1);
		for (int i = minlen; i >= 1; --i)
		{
			printf("%c", s1[i]);
		}
		printf("\n");
	}
	RE0
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值