Note that the question of simple Hash has already indicated that the number of NCS cannot exceed 16 Million. For details, see the implementation of the Code.
/******************** PRO: poj 1200 TIT: Crazy Search DAT: AUT: UKean EMA: huyocan@163.com *********************/# include <cstdio> # include <cstring> # define maxn 1005 bool has [16000006] = {0 }; int asc [300], n, nc, ans = 1, tag = 0, base = 0, chu = 1, num; int main () {scanf ("% d \ n", & n, & nc); memset (asc,-1, sizeof (asc )); // The ing table of initialization characters that maps nc different characters to 0 to the number of nc nc-1, // in this way, we can get the number of nc hexadecimal char temp = getchar (); for (in T I = 0; I <n-1; I ++) // use this number to modulo the n-bit nc hexadecimal number to remove its highest chu * = nc; for (int I = 0; I <n; I ++) // obtain the first n-bit nc hexadecimal number {num = (int) temp; if (asc [num] =-1) asc [num] = tag ++; // according to the order of appearance to nc different characters are labeled as 0 to nc-1 base = base * nc + asc [num]; temp = getchar ();} has [base] = 1; while (temp! = '\ N') {int num = (int) temp; if (asc [num] =-1) asc [num] = tag ++; // according to the order of the appearance of nc different characters are marked as 0 to nc-1 base = (base % chu) * nc + asc [num]; if (! Has [base]) // generates a number of normal nc hexadecimal values {ans ++; has [base] = 1 ;}temp = getchar ();} printf ("% d \ n", ans); return 0 ;}