POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200
题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同。
思路:字符串HASH,因为只有NC个不同的字符,所以我们可以把字符串看成是一个NC进制的串,然后计算出字符串的前缀HASH。然后枚举起点判断子串的HASH值是否已经存在。因为有了前缀HASH值,所以转移是O(1)的。
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<set>
using namespace std;
typedef long long int LL;
typedef unsigned int uint;
const int MAXN=+;
const int mod=+;
int Hash[MAXN],vis[];
char str[MAXN];
bool cnt[MAXN];
int pow_mod(int a,int b){
int ans=;
while (b)
{
if (b & )
ans = (1LL*ans*a)%mod;
b >>= ;
a = (1LL*a*a)%mod;
}
return ans;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
int hashNum=,ans=;
scanf("%s",str+);
memset(vis,-,sizeof(vis));
memset(cnt,false,sizeof(cnt));
int len=strlen(str+);
for(int i=;i<=len;i++){
if(vis[str[i]]==-){
vis[str[i]]=hashNum;
Hash[i]=hashNum++;
}
else{
Hash[i]=vis[str[i]];
}
}
int P=pow_mod(m,n-),pNum=;
for(int i=;i<=len;i++){
if(i>=n){
pNum=(((pNum-Hash[i-n]*P)*m+Hash[i])%mod+mod)%mod;
if(!cnt[pNum]){
cnt[pNum]=true;
ans++;
}
}
else{
pNum=(pNum*m+Hash[i])%mod;
}
}
printf("%d\n",ans);
}
return ;
}
POJ 1200 字符串HASH的更多相关文章
- poj 1200字符串hash
题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...
- poj 2503 字符串hash
题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<cstring> #include<i ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- POJ 3865 - Database 字符串hash
[题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- Palindrome POJ - 3974 (字符串hash+二分)
Andy the smart computer science student was attending an algorithms class when the professor asked t ...
- POJ 1200 Crazy Search 【hash】
<题目链接> 题目大意: 给定n,nc,和一个字符串,该字符串由nc种字符组成,现在要你寻找该字符串中长度为n的子字符串有多少种. 解题分析: 因为要判重,所以讲这些字符串hash一下,将 ...
随机推荐
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【Git】自定义Git
来源:廖雪峰 让Git显示颜色 git config --global color.ui true 忽略特殊文件 有些文件有敏感信息,或者是自动生成的中间文件.不能或不必提交到git,可以用.giti ...
- 用css解决iframe的自适应问题(跨域下同样有用)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- php字符串处理函数相关操作
<?php//获取tech和98426这两个字符串
- endnote设置文献第二行悬挂缩进办法
参考:http://blog.sina.com.cn/s/blog_62b13cf201014lfr.html 使用[endnote]插入文献后,如果文献稍长些,有第二行,第二行会顶格开始.并且这个 ...
- eclipse 注释模板
位置:window -> Preferences -> java -> Code Style -> Code Templates <?xml version=" ...
- Java遍历List的时候删除item
参考:http://blog.csdn.net/longyulu/article/details/8315068 在Java中有时候我们会需要对List里面的符合某种业务的数据进行删除,但是如果不了解 ...
- NYOJ之括号配对问题
括号配对问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0<N&l ...
- tableView 局部刷新
//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2]; [tableview reloadSections:in ...
- git 提交代码的流程
[root@ok-T IT-DOC]# ls hx-海星-wifi.rd web收藏.txt [root@ok-T IT-DOC]# git status -s ?? "web\346\22 ...