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一下,将 ...
随机推荐
- jquery-validation-1.13.1 自定义验证正则
/*** check Mobile***********************/ jQuery.validator.addMethod("isMobile", function( ...
- Quatre 2D的绘图功能的三个步骤(上下文,绘图,渲染)
一.qurza2d是怎么将绘图信息和绘图的属性绘制到图形上下文中去的? 说明: 新建一个项目,自定义一个view类和storyboard关联后,重写该类中的drowrect方法. 画线的三个步骤: ( ...
- OSG osgDB FileUtils FileNameUtil操作文件名相关函数
/** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */extern OSGDB_EXPORT std::stri ...
- Crystal Report 遇到需要登录的问题
解决方式: The advices for crystal report database connection settings: 1, Using ApplyLogOnInfo method in ...
- CABasicAnimation animationWithKeyPath 一些规定的值
CABasicAnimation animationWithKeyPath Types When using the ‘CABasicAnimation’ from the QuartzCore Fr ...
- RedHat下安装MySQL
下载mysql 解压tar -xvf mysql-5.7.16-1.el6.x86_64.rpm-bundle.tar 安装MySQL-server包 rpm -ivh mysql-community ...
- Android xml text 预览属性
只在 AS 中生效 xmlns:tools="http://schemas.android.com/tools" tools:text="I am a title&quo ...
- LNMP平台搭建---Nginx安装篇
在上一篇博文<LNMP平台搭建---Linux系统安装篇>中,我们安装了CentOS版本的Linux操作系统,现在,我们来安装一个Web服务器,大标题写着LNMP,其中的N就是Nginx, ...
- 三、jQuery--jQuery实践--瀑布流布局
实现方法: 1.JavaScript 2.jQuery 3.CSS多栏布局 法一: window.onload=function(){ waterfall('main','pin'); var dat ...
- wifi 4次握手
转自:http://zhaoxiaobu.blog.51cto.com/878176/407130/ 不管是用WEP加密,还是用WPA,一般如果我们要和AP建立一个连接,要经过两个阶段认证(Authe ...