Reincarnation hdu4622 hash解法

http://acm.hdu.edu.cn/showproblem.php?pid=4622

终于知道怎么hash解这题了,弱爆了==#


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <algorithm>
#include <utility>
#include <list>
#include <fstream>
#include <cctype>
#include <vector>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;

const int MAXN(2010);
const int MAXM(20010);
const int MAXE(210);
const int MAXK(6);
const int HSIZE(3131);
const int SIGMA_SIZE(40);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));

template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(T a){return a < 0? -a: a;}
template<typename T> inline bool EZ(T a){return ABS(a) < EPS;}

char str[MAXN];
ULL hash[MAXN];
ULL pow27[MAXN];
ULL geth(int l, int r){
    return hash[l]-hash[r+1]*pow27[r-l+1];
}

struct HASH_MAP{
    int first[HSIZE];
    ULL sta[MAXN];
    int vl[MAXN], vr[MAXN], next[MAXN];
    int size;
    void init(){
        memset(first, -1, sizeof(first));
        size = 0;
    }
    bool insert(ULL ts, int tl, int tr, int &l, int &r){
        int h = ts%HSIZE;
        for(int i = first[h]; ~i; i = next[i])
            if(ts == sta[i]){
                l = vl[i];
                r = vr[i];
                vl[i] = tl;
                vr[i] = tr;
                return false;
            }
        sta[size] = ts;
        vl[size] = tl;
        vr[size] = tr;
        next[size] = first[h];
        first[h] = size++;
        return true;
    }
} hm;

int dp[2010][2010];

int main(){
    pow27[0] = 1;
    for(int i = 1; i <= 2000; ++i) pow27[i] = pow27[i-1]*27;
    int TC;
    scanf("%d", &TC);
    while(TC--){
        scanf("%s", str);
        int len = strlen(str);
        memset(dp+1, 0, sizeof(dp[0])*len);
        hash[len] = 0;
        for(int i = len-1; i >= 0; --i) hash[i] = hash[i+1]*27+str[i]-'a'+1;
        for(int i = 1;i <= len; ++i){
            int cnt = 0;
            hm.init();
            for(int j = 0; j <= len-i; ++j){
                int l, r;
                if(!hm.insert(geth(j, j+i-1), j, j+i-1, l, r)) dp[len-l][j+i] += -1;
                dp[len-j][j+i] += 1;
            }
        }
        for(int i = 1; i <= len; ++i)
        {
            int ts = 0;
            for(int j = 1; j <= len; ++j){
                ts += dp[i][j];
                dp[i][j] = ts+dp[i-1][j];
            }
        }
        int Q;
        scanf("%d", &Q);
        while(Q--){
            int l, r;
            scanf("%d%d", &l, &r);
            printf("%d\n", dp[len-l+1][r]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值