hash 雜湊的簡單入門

2022-05-10 02:42:09 字數 1835 閱讀 3440

演算法競賽中的雜湊是往往是種種概率/玄學但是又賊雞兒快方便的神奇演算法

本質是通過對目標資料建立唯一對映進行縮小資料規模的權值判斷, 常常可以亂搞一些題目

對迴圈陣列的hash

解題思路: 對每個六元組求最小表示, 然後用乘積與和對應的雙hash對映入乙個鍊錶 再判斷即可

字串雜湊

子串hash模板題

最長回文子串hash o(n) 演算法 

再也不用記馬拉車了233

解法: 字串hash常使用進製轉化法, 具有類字首和的性質

modhash

const ull mod = 386910137;

ull pre[maxn];

ull fac[maxn] = ;

ull gethash(ull *pre, int a, int b)

fr(i, 1, 1e6 + 10) //預處理階乘

pre[flag] = (pre[flag - 1] * p % mod + x[j]) % mod; //預處理hash

自然溢位 

ull pre[maxn], fac[maxn] = ; //自然溢位法 

cin >> s + 1

for(int i = 1; i <= len; ++i) //建立hash table

ull getprehash(int a, int b) //查詢任意字串的hash值

最長回文子串的hash求法

/*

zeolim - an ac a day keeps the bug away

*///pragma gcc optimize(2)

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;

typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;

const ld pi = acos(-1.0);

const ld e = exp(1.0);

const int inf = 0x3f3f3f3f;

const int maxn = 1e6 + 10;

const ll mod = 1e9 + 7;

const ull p = 131;

ull pre[maxn], fac[maxn] = , pio[maxn];

char s[maxn];

ull getprehash(int a, int b)

ull getpiohash(int a, int b)

int main()

for(int i = len; i >= 1; --i)

for(int i = 1; i <= len; ++i)

rpis = apis;

while(i + 1 - rpis >= 1 && i + rpis <= len && getprehash(i - rpis + 1, i + 1) == getpiohash(i, i + rpis))

}cout << ans << '\n';

}return 0;

}

Hash 雜湊 雜湊

hash 一種用於查詢的資料結構 雜湊查詢的前提是已經一定的規則方法建好了雜湊表。基本思想是 關鍵字 位址轉換法 以資料物件的關鍵字為自變數,通過乙個確定的函式關係h,計算出對應的函式值h key 把這個值解釋為資料物件的儲存位址,並按此存放,即儲存位置 h key 關鍵 1.構造好的雜湊函式 2....

Redis入門之雜湊 Hash 型別

redis提供了雜湊型別,雜湊的值是乙個field和value的對映表,而獲取雜湊的鍵與上一節中字串型別中的鍵一樣.如下圖,描述了redis中雜湊型別 鍵名為 car 2 欄位名分別有 color name price 對應的欄位名為 白色 奧迪 90萬 通過上圖可以看出 欄位和字段值構成了乙個對映...

雜湊演算法(Hash函式)簡單介紹

雜湊演算法的定義和原理 將任意長度的二進位制值串對映為固定長度的二進位制值串,這個對映的規則,就是雜湊演算法。而通過原始資料對映之後得到的二進位制值串就是雜湊值。設計乙個優秀的雜湊演算法,需要滿足下面幾點要求 a.從雜湊值不能反向推導出原始資料 所以雜湊演算法也叫單向雜湊演算法 b.對輸入的資料比較...