• POJ 1200 Crazy Search【Hash入门】


     RK法:https://www.cnblogs.com/16crow/p/6879988.html

    #include<cstdio>
    #include<string>
    #include<cstdlib>
    #include<cmath>
    #include<iostream>
    #include<cstring>
    #include<set>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cctype>
    #include<stack>
    #include<sstream>
    #include<list>
    #include<assert.h>
    #include<bitset>
    #include<numeric>
    #define debug() puts("++++")
    #define gcd(a,b) __gcd(a,b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define fi first
    #define se second
    #define pb push_back
    #define sqr(x) ((x)*(x))
    #define ms(a,b) memset(a,b,sizeof(a))
    #define sz size()
    #define be begin()
    #define mp make_pair
    #define pu push_up
    #define pd push_down
    #define cl clear()
    #define lowbit(x) -x&x
    #define all 1,n,1
    #define rep(i,x,n) for(int i=(x); i<=(n); i++)
    #define in freopen("in.in","r",stdin)
    #define out freopen("out.out","w",stdout)
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int,int> P;
    const ULL base = 100000007;//33951943
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e18;
    const int maxn = 16000005+20;
    const int maxm = 1e6 + 10;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int dx[] = {-1,1,0,0,1,1,-1,-1};
    const int dy[] = {0,0,1,-1,1,-1,1,-1};
    int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int t,n,m;
    char s[maxn];
    int has[maxn],a[150];
    int main()
    {
       while(~scanf("%d%d",&n,&m))//将字符串对应到m进制数
       {
           set<int> st;
           ms(a,0),ms(has,0);
           int cnt=0,sum=0,num=0;
           scanf("%s",s);
           int len = strlen(s);
           //按照字符出现的先后顺序确定字符的大小
           for(int i=0;i<len;i++)//根据字符出现的顺序,给字符标号1,2...
           {
               if(!a[s[i]])
                a[s[i]]=++num;
           }
    
           for(int i=0;i<len;i++)
            printf("a[%d] = %d
    ",s[i],a[s[i]]);
    
           for(int i=0;i+n-1<len;i++) //把子串映射到hash数组中
           {
               sum=0;
               for(int j=i;j<i+n;j++)//子串长度
               {
                   sum=sum*m+a[s[j]]; //将长度为n的子串看作是n位m进制数,这里求的是这个n位m进制数的对应的十进制数
                   printf("s[%d]=%d a[s[j]]=a[%d]=%d sum=%d
    ",j,s[j],s[j],a[s[j]],sum);
    
               }
               //st.insert(sum);
               printf("SUM = %d
    ",sum);
               if(!has[sum])//has[sum]==0表示没有没有出现过
               {
                   cnt++; //不同串的个数加1
                   has[sum]=1;
               }
           }
           printf("cnt = %d
    ",cnt);
       }
    }
    /*
    【题意】
    把出现过的每个字母映射到对应的数字,这样字符串就变成相应的m进制数,然后把它转换成10进制,并放入has[]中,如果是第一次放入,则总数加一
    n m len=8
    3 4
    daababac
    a[d] = 1
    a[a] = 2
    a[b] = 3
    a[c] = 4
    daa=122(4进制)
    转换为十进制: daa = 1 * 4 ^ 2 + 2 * 4 ^ 1 + 2 * 4 ^ 0 = 16+8+2=26
    然后if(!hash[26]) hash[26]++;
    
    【类型】
    
    【分析】
    
    【时间复杂度&&优化】
    
    【trick】
    对字符串hash的优化就是,重新定义各个字母的编号,而不是直接用ASCII码值
    
    【数据】
    3 4
    daababac
    a[100] = 1
    a[97] = 2
    a[97] = 2
    a[98] = 3
    a[97] = 2
    a[98] = 3
    a[97] = 2
    a[99] = 4
    s[0]=100 a[s[j]]=a[100]=1 sum=1
    s[1]=97 a[s[j]]=a[97]=2 sum=6
    s[2]=97 a[s[j]]=a[97]=2 sum=26
    SUM = 26
    s[1]=97 a[s[j]]=a[97]=2 sum=2
    s[2]=97 a[s[j]]=a[97]=2 sum=10
    s[3]=98 a[s[j]]=a[98]=3 sum=43
    SUM = 43
    s[2]=97 a[s[j]]=a[97]=2 sum=2
    s[3]=98 a[s[j]]=a[98]=3 sum=11
    s[4]=97 a[s[j]]=a[97]=2 sum=46
    SUM = 46
    s[3]=98 a[s[j]]=a[98]=3 sum=3
    s[4]=97 a[s[j]]=a[97]=2 sum=14
    s[5]=98 a[s[j]]=a[98]=3 sum=59
    SUM = 59
    s[4]=97 a[s[j]]=a[97]=2 sum=2
    s[5]=98 a[s[j]]=a[98]=3 sum=11
    s[6]=97 a[s[j]]=a[97]=2 sum=46
    SUM = 46
    s[5]=98 a[s[j]]=a[98]=3 sum=3
    s[6]=97 a[s[j]]=a[97]=2 sum=14
    s[7]=99 a[s[j]]=a[99]=4 sum=60
    SUM = 60
    cnt = 5
    
    */
    View Code
  • 相关阅读:
    【二食堂】Alpha
    【二食堂】Alpha- 发布声明
    【Beta】Scrum Meeting 4
    【Beta】Scrum Meeting 3
    【Beta】Scrum Meeting 2
    【Beta】Scrum Meeting 1
    beta设计和计划
    alpha事后分析
    alpha项目展示
    Scrum Meeting 最终总结
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9447830.html
Copyright © 2020-2023  润新知