Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- const long long p = 31;
- const long long mod = 1e9 + 7;
- map<int,set<int>> hs;
- long long single_hash (string & s) {
- vector<long long> pows;
- pows.push_back(1);
- int n = (int) s.size();
- for (int i = 0; i < n; ++i) {
- pows.push_back((pows.back() * p) % mod);
- }
- vector<long long> suf_hash,pref_hash;
- pref_hash.push_back(((s[0] - 'a' + 1) * pows[0]) % mod);
- for (int i = 1; i < n; ++i) {
- pref_hash.push_back((pref_hash.back() + (s[i] - 'a' + 1) * pows[i]) % mod);
- }
- suf_hash.push_back(((s[n - 1] - 'a' + 1) * pows[n - 1]) % mod);
- for (int i = n - 2; i >= 0; --i) {
- suf_hash.push_back((suf_hash.back() + (s[i] - 'a' + 1) * pows[i]) % mod);
- }
- cout << pref_hash[n - 1] << ' ' << suf_hash[0] << '\n';
- return pref_hash[n - 1];
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- string h = "aaaaa";
- cout << single_hash(h);
- //~ int T = 1;
- //~ cin >> T;
- //~ for (int test_case = 1; test_case <= T; ++test_case) {
- //~ int n,m;
- //~ cin >> n >> m;
- //~ for (int i = 1; i <= n; ++i) {
- //~ string s;
- //~ cin >> s;
- //~ cout << single_hash(s) << '\n';
- //~ hs[(int) s.size()].insert(single_hash(s));
- //~ }
- //~ for (int i = 1; i <= m; ++i) {
- //~ string s;
- //~ cin >> s;
- //~ cout << single_hash(s) << '\n';
- //~ }
- //~ }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement