美文网首页
字符串hash

字符串hash

作者: 徐振杰 | 来源:发表于2019-05-19 16:08 被阅读0次

兔子和兔子

#include<iostream>
#include<string.h>
using namespace std;
typedef unsigned long long ull;
const int N = 1000010,base = 131;
ull h[N],p[N];
char s[N];

ull get(int i,int j){
    return h[j] - h[i-1]*p[j-i+1];
}

int main(){
    scanf("%s",s+1);
    p[0] = 1;
    int size = strlen(s+1);
    for(int i=1;i<=size;i++){
        h[i] = h[i-1]*base + s[i] - 'a' +1;
        p[i] = p[i-1] * base;
    }
    

    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int l1,r1,l2,r2;
        cin>>l1>>r1>>l2>>r2;
        ull s1 = get(l1,r1);
        ull s2 = get(l2,r2);

        if(s1==s2)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
    return 0;
}

最大回文子串

#include<iostream>
#include<string.h>
using namespace std;

typedef unsigned long long ull;
const int N = 2000010,base = 131;
ull p[N],hl[N],hr[N];
char s[N];

ull getl(int i,int j){
    return hl[j] - hl[i-1]*p[j-i+1];
}
ull getr(int i,int j){
    return hr[i] - hr[j+1]*p[j-i+1];
}

bool check(int x,int center){
    ull l = getl(center-x,center-1);
    ull r = getr(center+1,center+x);
    if(l==r) return true;
    return false;
}

int main(){
    int C = 0;
    while(scanf("%s",s+1),strcmp(s+1,"END")){
        C++;
        int n = strlen(s+1);
        for(int i = 2*n;i>0;i-=2){
            s[i] = s[i/2];
            s[i-1] = 'z'+1;
        }
        
        n = 2*n;

        p[0] = 1;
        for(int i=1,j=n;i<=n;i++,j--){
            hl[i] = hl[i-1]*base + s[i]-'a'+1;
            hr[j] = hr[j+1]*base + s[j]-'a'+1;
            p[i] = p[i-1]*base;
        }
        int res = 1;
        for(int i=1;i<=n;i++){
            int len = min(i-1,n-i);
            
            int l = 0,r= len;
            while(l-r){
                int mid = l+r+1>>1;
                if(check(mid,i)) l = mid;
                else r = mid -1;
            }

            if(s[i+l]>'z') res = max(res,l);
            else{
                res = max(res,l+1);
            }
        }
        
        printf("Case %d: %d\n",C,res);
    }
    return 0;
}

kmp
周期

#include<iostream>

using namespace std;
const int N = 1000010;
char s[N];
int Next[N];
int n;
void get_next(){
    int j = 0;
    for(int i=2;i<=n;i++){
        while(j&&s[i]!=s[j+1]) j = Next[j];
        if(s[i]==s[j+1]) j++;
        Next[i] = j;
    }
}

int main(){
    int C = 0;
    while(scanf("%d",&n),n){
        C++;
        scanf("%s",s+1);
        get_next();
        
        cout<<"Test case #"<<C<<endl;
        for(int i= 1;i<=n;i++){
            int t = i-Next[i];
            
            if(t!=i&&i%t==0) cout<<i<<" "<<i/t<<endl;
        }
        cout<<endl;
    }

}

相关文章

  • 字符串---hash

    字符串hash 一个hash方法,BKDRHash。选定一个seed,然后求一个字符串的hash值for(int ...

  • redis常用操作

    目录 字符串 List Hash Set 有序集合 key通用操作 1. 字符串 2. List 3. Hash ...

  • hash实现锚点平滑滚动定位

    一、科普时间 hash hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分)...

  • Redis(5.redis的数据结构及常见指令)

    五种数据类型: 字符串(String) 字符串列表(list) 哈希(hash) 字符串集...

  • 机试常用算法和题型-哈希专题

    哈希专题 hash表的用法 hash表高阶用法,二维数组存放不同组的hash值 hash结合字母表处理字符串的使用方法

  • Redis数据类型和结构

    数据类型 string,list,set,hash,zset 数据结构 动态字符串 ziplist hash表 l...

  • Redis系列第三篇之Hash

    前言 Redis的Hash是字符串类型的字段和字符串类型的值之间的映射,所以Hash是用于表示对象的完美数据类型(...

  • 算法入门:Hash

    什么是Hash算法:##### 简单的说,hash算法就是将字符串转化为数字的算法。 用一个例子说Hash的优势#...

  • redis数据结构

    五种数据结构 字符串(String) 哈希(hash) 字符串列表(list) 字符串集合(set) 有序字符串集...

  • Redis 常用5大数据类型及应用场景

    Redis最为常用的数据类型: 字符串(String) 哈希(hash) 字符串列表(list) 字符串集合(se...

网友评论

      本文标题:字符串hash

      本文链接:https://www.haomeiwen.com/subject/bfdqzqtx.html