首页 > 其他分享 >字符串哈希板子

字符串哈希板子

时间:2024-04-07 23:02:54浏览次数:24  
标签:std hash unsigned long 板子 哈希 字符串 array size

#include <iostream>
#include <cstring>

#define MAX_SIZE 100
using namespace std;
class StringHash {

public:
    int size;
    char *array;
    char *array_forward;
    unsigned long long* pre_base;
    unsigned long long* hash_array;
    unsigned long long* hash_array_forward;
    unsigned long long base = 12582917;

        // 构造函数
    StringHash(int size) : size(size) {
        array = new char[size + 1]; 
        pre_base = new unsigned long long[size + 1];
        hash_array = new unsigned long long[size + 1];
        std::memset(array, 0, size + 1); 
        std::memset(pre_base, 0, size + 1); 
        std::memset(hash_array, 0, size + 1); 
    }

    // 析构函数
    ~StringHash() {
        delete[] array;
        delete[] hash_array;
        delete[] pre_base;
    }


    friend std::istream& operator>>(std::istream& is, StringHash& customStruct) {
        is.getline(customStruct.array+1, customStruct.size + 1); // Read a line from input stream
        return is;
    }

    friend std::ostream& operator<<(std::ostream& os, const StringHash& customStruct) {
        os << customStruct.array+1; // Output the array to the output stream
        return os;
    }

    void init()
    {
        pre_base[0]=1;
        for(int i=1;i<=size;i++) 
        pre_base[i]=base*pre_base[i-1];
         
        hash_array[0]=0;
        for(int i=1;i<=size;i++)
        {
            hash_array[i]=hash_array[i-1]*base;
            hash_array[i]+=array[i];
        }

    }

    void open_parall()
    {
        hash_array_forward = new unsigned long long[size + 1];
        array_forward = new char[size + 1];
        std::memset(hash_array_forward, 0, size + 1); 
        std::memset(array_forward, 0, size + 1); 
        for(int i=size;i>=1;i--)
        {
            array_forward[i]=array[size-i+1];
        }
        for(int i=1;i<=size;i++)
        {
            hash_array_forward[i]=hash_array_forward[i-1]*base;
            hash_array_forward[i]+=array_forward[i];
        }
    }
    
    unsigned long long getHash(int l,int r)
    {
        return hash_array[r]-hash_array[l-1]*pre_base[r-l+1];
    }

    bool is_paralld(int l,int r)
    {
        return getHash(l,r)==getHash(size-r+1,size-l+1,hash_array_forward);
    }
    
    unsigned long long getHash(int l,int r,unsigned long long* Hash)
    {
       return Hash[r]-Hash[l-1]*pre_base[r-l+1];
    }

};

int main() {
    int desiredSize = 4; // Example size

    StringHash stringA(desiredSize);

    cin>>stringA;
    cout<<stringA<<endl;
    stringA.init();
    stringA.open_parall();

    int l,r;
    while(1)
    {
        cin>>l>>r;
        cout<<stringA.is_paralld(l,r)<<endl;
        //cout<<stringA.getHash(l,r)<<endl;
    }

    // Optionally, you can manipulate the array here

    return 0;
}

 

标签:std,hash,unsigned,long,板子,哈希,字符串,array,size
From: https://www.cnblogs.com/acmLLF/p/18120116

相关文章

  • python学习--基础知识(字符串扩展)
    八、字符串扩展1、字符串的三种定义方式2、字符串的拼接3、字符串的格式化4、字符串格式化的精确度控制5、字符串格式化的快速方法6、字符串格式化--对表达式进行格式化......
  • python 字符串的操作
    #字符串拼接str1="Hello"str2="World"combined_str=str1+""+str2print(combined_str)#字符串重复str1="Python"repeated_str=str1*3print(repeated_str) #根据字符串索引取值str1="Hello"char=str1[1]#......
  • 如何在 Node.js 中使用 bcrypt 对密码进行哈希处理
    在网页开发领域中,安全性至关重要,特别是涉及到用户凭据如密码时。在网页开发中至关重要的一个安全程序是密码哈希处理。密码哈希处理确保明文密码在数据库受到攻击时也难以被攻击者找到。但并非所有的哈希方法都是一样的,这就是bcrypt突出之处所在。Node.js是一个流行的用于开......
  • 两个字符串间的最短路径问题【华为OD机试】(JAVA&Python&C++&JS题解)
    一.题目-两个字符串间的最短路径问题给定两个字符串,分别为字符串A与字符串B。例如A字符串为ABCABBA,B字符串为CBABAC可以得到下图m*n的二维数组,定义原点为(0,0),终点为(m,n),水平与垂直的每一条边距离为1,映射成坐标系如下图。从原点(0,0)到(0,A)为水平边,距离为1,从(0,A)......
  • 【字符串】Manacher
    Manacher算法的本质是计算以字符串中的“每个字符”和“每两个相邻字符之间的空隙”作为对称中心的最大回文串的长度。所以利用这个性质可以解决一系列与子串是否是回文串、子串有多少是回文串的问题。namespaceManacher{constintMAXN=1.1e7;intn;chars[MAXN+10];......
  • aardio教程五) 写Python风格的aardio代码(字符串篇)
    前言熟悉一个新的语言最麻烦的就是需要了解一些库的使用,特别是基础库的使用。所以我想给aardio封装一个Python风格的库,Python里的基础库是什么方法名,aardio里也封装同样的方法名。这样就不需要单独去了解aardio里一些方法的使用细节,可以很方便的将Python代码改成aardio代码。......
  • 一致性哈希
    一致性哈希  一、什么是一致性哈希  一致性哈希是一种用于分布式系统中数据分片和负载均衡的算法。它的核心思想是将数据和节点通过哈希函数映射到一个固定范围的环形空间中,每个节点在环上占据一个位置,而数据则根据哈希函数计算出的值映射到环上的某个位置上。目前主要应......
  • C++基础——字符串(C语言和C++的字符串风格区别)
    C语言风格的字符串字符串通常是以字符数组或字符指针的形式表示的。字符串以空字符('\0')结尾!!!两种形式:(1)字符指针形式的字符串charstr[]="HelloC++";(2)字符数组形式的字符串char*ptr="HelloC++";C风格字符串的运算运算需要使用string函数,需要加入头文件<stri......
  • P3396 哈希冲突
    原题链接题解正常暴力解法如下:#include<bits/stdc++.h>usingnamespacestd;inta[150007];intmain(){intn,m;cin>>n>>m;for(inti=1;i<=n;i++)cin>>a[i];while(m--){charop;cin>>op;i......
  • c++算法学习笔记 (20) 哈希表
    1.模拟散列表//拉链法#include<bits/stdc++.h>usingnamespacestd;constintN=100003;inth[N];inte[N],ne[N],idx;//存链voidinsert(intx){intk=(x%N+N)%N;//让负数的余数变成正数(若直接加N,则可能溢出)e[idx]=x;ne[idx]......