参考:https://www.cnblogs.com/My-Sunshine/p/4934362.html
https://blog.csdn.net/tomcmd/article/details/48101857
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=16*1e6+10; 6 int n,nc; 7 int hashmap[300];//hash表,存字符的nc进制数 8 bool vis[N];//标记查重 9 char s[N]; 10 int main() 11 { 12 // freopen("bin.txt","r",stdin); 13 while (scanf("%d %d",&n,&nc)!=EOF) 14 { 15 scanf("%s",s); 16 memset(vis,0,sizeof(vis)); 17 memset(hashmap,0,sizeof(hashmap)); 18 int len=strlen(s); 19 int k=0; 20 for (int i=0;i<len;i++) 21 { 22 if (!hashmap[s[i]])//转换为nc进制数 23 { 24 hashmap[s[i]]=k++; 25 } 26 } 27 int ans=0; 28 for (int i=0;i<len-n+1;i++) 29 { 30 int temp=0; 31 for (int j=i;j<i+n;j++) 32 { 33 temp=temp*nc+hashmap[s[j]];//注意转化公式不要写错! 34 } 35 if (!vis[temp]) 36 { 37 ans++; 38 vis[temp]=true; 39 } 40 } 41 printf("%d ",ans); 42 } 43 44 return 0; 45 }