首页 > 其他分享 >Template <字符串哈希>

Template <字符串哈希>

时间:2023-07-27 20:44:20浏览次数:40  
标签:Template int vector ULL include StringHash string

#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ULL = unsigned long long;

// 字符串哈希(注意 get(l,r)为闭区间,字符串下标从1开始)
struct StringHash
{
    vector<ULL> h;  // 哈希数组
    vector<ULL> p;  // p[i] = P^i
    int n;          // 字符串长度
    ULL P;          // 进制数
    StringHash(string _s, ULL _P = 131)
    {
        P = _P;
        init(_s);
    }
    void init(string const &s)
    {
        n = s.size();
        h = vector<ULL>(n + 1);
        p = vector<ULL>(n + 1);
        h[0] = 0;
        p[0] = 1;
        for (int i = 1; i <= n; i ++)
        {
            p[i] = p[i-1] * P;
            h[i] = h[i-1] * P + s[i-1];
        }
    }
    ULL get(int l, int r)
    {
        return h[r] - h[l - 1] * p[r - l + 1];
    }
    
};

int main()
{
    int T;
	cin >> T;
    string s;
	cin >> s;
	StringHash h(s);
    int l1, l2, r1, r2;
    while (cin >> l1 >> r1 >> l2 >> r2)
    {
        cout << h.get(l1, r1) << ", " << h.get(l2, r2) << ", ";
        if (h.get(l1, r1) == h.get(l2, r2)) puts("Yes");
        else puts("No");
    }
    return 0;
}

标签:Template,int,vector,ULL,include,StringHash,string
From: https://www.cnblogs.com/o2iginal/p/17585969.html

相关文章

  • Template <Manacher>
    #include<iostream>#include<string>#include<vector>usingnamespacestd;//O(n)计算字符串s的每个字符的最大回文半径,返回最长回文子串长度intManacher(strings){ //空字符串直接返回0if(s.length()==0)return0; //manacher扩展后串长度int......
  • String mobleCode = redisTemplate.opsForValue().get(phone);
    使用RedisTemplate获取手机验证码在现代的应用程序中,手机验证码被广泛用于用户身份验证和安全验证。使用手机验证码可以确保用户提供的手机号是有效的,并且可以防止恶意行为。在本文中,我们将介绍如何使用SpringDataRedis中的RedisTemplate来获取手机验证码。RedisTemplate简介R......
  • 在langchain中使用带简短知识内容的prompt template
    简介langchain中有个比较有意思的prompttemplate叫做FewShotPromptTemplate。他是这句话的简写:"Prompttemplatethatcontainsfewshotexamples."什么意思呢?就是说在Prompttemplate带了几个比较简单的例子。然后把这些例子发送给LLM,作为简单的上下文环境,从而为LLM提供额外......
  • cookie+session(这里使用redistemplate代替)实现单点登录流程
     user发起资源请求(带上回调的路径方便回调),通过判断是否浏览器的cookie中是否存在登录过的痕迹,比如有人登了,然后存了一个cookie到浏览器如果拿到了cookie是有东西的,则带上这个cookie的内容返回给client,如果没有东西,则继续登录,向session中存入userInfo,并给浏览器设置cookie......
  • 封装RedisTemplate工具类
    packagecom.juxi.common.redis.service;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.*;importorg.springframework.stereotype.Component;importjava.time.Duration;importjava.util.*;importja......
  • modulo template
    #include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;template<classT>constexprTpower(Ta,i64b){  Tres=1;  for(;b;b/=2,a*=a){    if(b%2){      res*=a;    }  }  returnr......
  • vSphere通 python vmtemplate
    vSphere通pythonvmtemplate介绍vSphere是一款由VMware开发的虚拟化平台,可用于创建和管理虚拟机。借助vSphereAPI,我们可以使用Python编写脚本来与vSphere进行交互。本文将介绍如何使用Python及其相关库来管理vSphere中的虚拟机模板。准备工作要开始使用Python与vSphere进行交......
  • RedisTemplate 泛型不同 指向的是同一个实例吗
    RedisTemplate泛型不同指向的是同一个实例吗在使用RedisTemplate时,我们经常会遇到需要指定不同数据类型的情况。比如,我们可能需要将某个对象存储到Redis中,并且需要使用不同的数据类型进行序列化和反序列化。那么,RedisTemplate在这种情况下会创建多个实例吗?本文将解答这个问......
  • 记jdbcTemplate使用的一个坑
    1、在使用jdbcTemplate时,语句不能使用select* ,不然可能就是这样的错误:Incorrectcolumncount:expected1,actual62、如果像这样的外层嵌套,应该去掉外层select*,语句:select*from(selectmater_score.mater_noasmaterNo,city,town,mater_score.avgasavgScor......
  • jdbc-plus是一款基于JdbcTemplate增强工具包,基于JdbcTemplate已实现分页、多租户、动
    ......