Poj--1200(字符串,hash)
2014-12-12 21:24:36
思路:hash启蒙题,相当于把一串字符串的所有字符的ASCII码看成NC进制数,然后暴力计数,题目很裸。。
1 /************************************************************************* 2 > File Name: 1200.cpp 3 > Author: Natureal 4 > Mail: 564374850@qq.com 5 > Created Time: Fri 12 Dec 2014 09:15:40 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 const int maxn = 16000010; 27 28 int N,NC; 29 char s[maxn]; 30 int hash[maxn]; 31 int lable[200]; 32 int ans,cnt; 33 34 int main(){ 35 scanf("%d%d",&N,&NC); 36 scanf("%s",s); 37 int len = strlen(s); 38 for(int i = 0; i < len; ++i){ 39 if(!lable[s[i]]) 40 lable[s[i]] = ++cnt; //给每个字符编号 41 } 42 for(int i = 0; i + N - 1 < len; ++i){ 43 int num = 0; 44 for(int j = i; j < i + N; ++j){ 45 num = num * NC + lable[s[j]]; 46 } 47 if(!hash[num]){ 48 hash[num] = 1; 49 ans++; 50 } 51 } 52 printf("%d\n",ans); 53 return 0; 54 }