DNA序列 LOJ NOIP模拟赛 D1T1 字符串哈希

字符串哈希

上代码

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
template<class T> inline void read(T &_a){
    bool f=0;int _ch=getchar();_a=0;
    while(_ch<'0' || _ch>'9'){if(_ch=='-')f=1;_ch=getchar();}
    while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();}
    if(f)_a=-_a;
}

char str[5000001];
int k,n,ans,cnt[50000000],jc[11];
string tmp;

inline int getid(char s)
{
    if(s=='A') return 1;
    if(s=='G') return 2;
    if(s=='C') return 3;
    return 4;
}

inline int h()
{
    int res=0;
    for (register int i=k-1;i>=0;--i)
        res+=getid(tmp[i])*jc[k-i];
    return res;
}

int main()
{
    jc[0]=1;
    for (register int i=1;i<=10;++i)
        jc[i]=jc[i-1]*5;
    scanf("%s",str+1);
    read(k);
    n=strlen(str+1);
    for (register int i=1;i+k<=n+1;++i)
    {
        tmp="";
        for (register int v=0;v<k;++v)
            tmp+=str[i+v];
        ans=max(ans,++cnt[h()]);
    }
    printf("%d",ans);
    return 0;
}

 

posted @ 2017-11-05 21:41  JayWang  阅读(244)  评论(0编辑  收藏  举报