poj 1200(字符串hash)

本文提供了一种解决 POJ 1200 题目的算法思路,通过将字符串问题转化为进制表示来寻找所有不同的子串。使用 C++ 实现,代码简洁高效。

题目链接:http://poj.org/problem?id=1200

题目大意:找不同的子串

题目分析:多不错的水题,适合我等菜鸟拉。转化为nc的进制,意味着有ans个不同个数

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
const int maxn=16*1e6+5;
using namespace std;
char string[maxn];
int letter[30];
bool hash [20000000];
long long sum;
int main()
{
    int n,nc,i,j,ans,len,count;
    while(scanf("%d %d",&n,&nc)==2)
    {
        scanf("%s",string);ans=0,count=1;
        len=strlen(string);
        memset(hash,false,sizeof(hash));
        memset(letter,0,sizeof(letter));
        for(i=0;i<=len-n;i++)
        {
            sum = 0;
            for(j=i;j<i+n;j++)
            {
               if(!letter[string[j]-'a']) letter[string[j]-'a']=count++;
               sum*=nc;sum+=letter[string[j]-'a'];
            }
            if(!hash[sum]){hash[sum]=true;ans++;}
        }
        printf("%d\n",ans);
    }
    return 0;
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值