zoukankan      html  css  js  c++  java
  • POJ-1200(哈希)

    2015-08-19

    题意:给出两个数n,nc,并给出一个由nc种字符组成的字符串。求这个字符串中长度为n的子串有多少种。
    分析:
    1.这个题不用匹配,因为不高效。
    2.将长度为n的子串看作n位的nc进制数,将问题转化为共有多少种十进制数字。
    3.哈希时,每一个字符都对应这0---nc-1的一个数字。
    代码:
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<map>
     6 using namespace std;
     7 const int maxn = 16e6;
     8 
     9 char s[maxn];
    10 unsigned int Hash[maxn];
    11 int n,m;
    12 int name[1000];
    13 
    14 unsigned int hash(char *str,int x)
    15 {
    16     unsigned int ans = 0;
    17     for(int i = 0; i < x; ++i)
    18     {
    19         ans=ans*m +name[*(str+i)];
    20     }
    21     return ans;
    22 }
    23 
    24 int main()
    25 {
    26     //freopen("in","r",stdin);
    27     while(~scanf("%d%d",&n,&m))
    28     {
    29         scanf("%s",s);
    30         int len = strlen(s),k = 0;
    31         for(int i = 0; i < len; ++i)
    32         {
    33             if(name[s[i]] == 0) name[s[i]] = k++;
    34         }
    35         for(int i = 0; i <= len - n; ++i)
    36         {
    37             unsigned int ID = hash(s+i,n);
    38             Hash[ID] = 1;
    39         }
    40         int ans = 0;
    41         for(int i = 0; i < maxn; ++i)
    42             ans += Hash[i];
    43         printf("%d
    ",ans);
    44     }
    45 }
    View Code
  • 相关阅读:
    UVA 125 Numbering Paths
    UVA 515 King
    UVA 558 Wormholes
    UVA 10801 Lift Hopping
    UVA 10896 Sending Email
    SGU 488 Dales and Hills
    HDU 3397 Sequence operation
    数据库管理工具navicat的使用
    javascript封装animate动画
    关于webpack没有全局安装下的启动命令
  • 原文地址:https://www.cnblogs.com/Norlan/p/4743294.html
Copyright © 2011-2022 走看看