poj1200 string hash

I still don't quite understand that there is a bug, why it must be nc base

First post an ac slow (94ms) code

#include <iostream>
#include  <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int n, m;
const int maxn = 16000001;
char a[maxn];
bool mp[maxn];
int name[300];
int has;
ull p, h;
int main() {
    scanf("%d%d", &n, &m);
    scanf("%s", a + 1);
    h = 0, p = 1;
    has = m;
    int len = strlen(a + 1);
    int k = 0;
    for(int i = 1; i <= len; i++) {
        if(name[a[i]] == 0) name[a[i]] = ++k;
        if(k == m) break;
    }
    int cnt = 0;
    for(int i = 1; i + n - 1 <= len; i++) {
        ull sum = 0;
        for(int j = i; j <= i + n - 1; j++) {
//            sum = sum * has + a[j];
sum=sum*has+name[a[j]];
        }
        if(!mp[sum]) {
            mp[sum] = 1;
            cnt++;
        }
    }
    printf("%d\n", cnt);
    return 0;
}

Paste another 32ms

#include <iostream>
#include  <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
int n, m;
const int maxn = 16000001;
char a[maxn];
bool mp[maxn];
int name[300];
int has;
ull p, h;
int main() {
    scanf("%d%d", &n, &m);
    scanf("%s", a + 1);
    h = 0, p = 1;
    has = m;
    int len = strlen(a + 1);
    int k = 0;
    for(int i = 1; i <= len; i++) {
        if(name[a[i]] == 0) name[a[i]] = ++k;
        if(k == m) break;
    }
    int cnt = 0;
    ull sum = 0;
    for(int i = 1; i <= n; i++) {
        p *= has;
        sum = sum * has + name[a[i]];
    }
    cnt++;
    mp[sum] = 1;
    for(int i = 1 + n; i <= len; i++) {

//        for(int j = i; j <= i + n - 1; j++) {
////            sum = sum * has + a[j];
//            sum = sum * has + name[a[j]];
//        }
        sum = sum * has + name[a[i]] - p * name[a[i - n]];
        if(!mp[sum]) {
            mp[sum] = 1;
            cnt++;
        }
    }
    printf("%d\n", cnt);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691341&siteId=291194637