[POJ1200] Crazy Search

题目

未知长度字符串询问有多少个不同的N长度子串,给出字符串字符集大小NC

题解

将长度为N的子串转化为NC进制数,用bool数组判重。

代码

/// by ztx
/// blog.csdn.net/hzoi_ztx
// #include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define rep(i,l,r) for(i=(l);i< (r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
#define rev(i,r,l) for(i=(r);i> (l);i--)
#define Each(i,v)  for(i=v.begin();i!=v.end();i++)
#define r(x)   read(x)
typedef long long ll ;
typedef double lf ;
int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
    ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
    if (CH == '-') NEG = true , CH = getchar() ;
    while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
    if (NEG) ret = -ret ;
}
template <typename TP>inline void reads(TP *ret) {
    ret[0]=0;while (CH=getchar() , CH<'!') ;
    while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ;
    ret[ret[0]+1]=0;
}

#define  kK  16000010LL

int n, m, ans = 0, to[512], now = 0;
bool hash[kK] = {false};
char s[kK];

int main() {
    int i;
    ll j = 0, k;
    r(m), r(n), scanf("%s", s+1);
    memset(to,-1,sizeof to);
    n = strlen(s+1);
    Rep (i,1,n)
        if (to[s[i]] == -1) to[s[i]] = now ++ ;
    k = 1;
    rep (i,1,m) k *= now;
    Rep (i,1,n) {
        if (i > m) j -= to[s[i-m]]*k;
        j = j*now+to[s[i]];
        if (i>=m && !hash[j]) hash[j] = true, ans ++ ;
    }
    printf("%d\n", ans);
    END: getchar(), getchar();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值