poj 1200字符串hash
题意:给出不同字符个数和子串长度,判断有多少个不同的子串
思路:字符串hash。
用字符串函数+map为什么会超时呢??
代码:
#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
const int N=16000005; //题目给出子串的最大和不超过16M
const int NUM=257;
bool hash[N];
int m[NUM];
char str[1000000];
int main()
{
int n,nc,i,j,sum,seed=0,ans=0;
memset(hash,false,sizeof(hash));
memset(m,0,sizeof(m));
memset(str,'\0',sizeof(str));
cin>>n>>nc>>str;
for(i=0;'\0' != str[i];++i)
{
if(!m[str[i]]) //将每个字符赋值为相应进制的数
m[str[i]]=++seed;
if(seed == nc)
break;
}
int len=strlen(str);
for(i=0;i<=len-n;++i)
{
sum=0;
for(j=0;j<n;++j) //将字符串str[i],..,str[i+n-1]变为一个nc进制的整数,来判断是否重复出现过
sum=sum*nc+m[str[i+j]]-1;
if(!hash[sum])
{
hash[sum]=true;
++ans;
}
}
cout<<ans<<endl;
return 0;
}
hash函数+map:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
#define MAXN 16000010
char str[MAXN];
map<int,bool> hash;
int n,nc; unsigned int hashValue(char *str , char *end)
{
unsigned int hash = 0;
int i;
for (i=0; str < end; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
}
else
{
hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
}
}
return (hash & 0x7FFFFFFF);
} int main()
{
while(scanf("%d%d",&n,&nc)!=EOF)
{
scanf("%s",str);
int len=strlen(str);
char *s=str;
char *t=s+len;
for(;s<=t-n;s++)
{
int v=hashValue(s,s+n);
hash[v]=true;
}
printf("%d\n",hash.size());
}
return 0;
}
poj 1200字符串hash的更多相关文章
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- 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一下,将 ...
随机推荐
- vue-router的使用
关于vue-router的基本使用方法 首先,需要下载vue-router npm install vue-router --save vue-router在html或组件中的展现 ``` &l ...
- JAVA中extends 与implements的用法
1. 在类的声明中,通过关键字extends来创建一个类的子类.一个类通过关键字implements声明自己使用一个或者多个接口.extends 是继承某个类, 继承之后可以使用父类的方法, 也可以重 ...
- promise处理多个相互依赖的异步请求
在项目中,经常会遇到多个相互依赖的异步请求.如有a,b,c三个ajax请求,b需要依赖a返回的数据,c又需要a和b请求返回的数据.如果采用请求嵌套请求的方式自然是不可取的.导致代码难以维护,如何请求很 ...
- mysql中varchar和char区别(思维导图整理)
由于mysql一直是我的弱项(其实各方面我都是很弱的),所以最近在看msyql,正好看到varchar和char区别,所以整理一下,便于以后遗忘. 0.0图片已经说明一切,但是系统说我字数不够,我真能 ...
- JavaScript闭包,只学这篇就够了
# 闭包不是魔法 这篇文章使用一些简单的代码例子来解释JavaScript闭包的概念,即使新手也可以轻松参透闭包的含义. 其实只要理解了核心概念,闭包并不是那么的难于理解.但是,网上充斥了太多学术性的 ...
- [ZYNQ-7]PS处理PL外部中断的简单实例的剖析 (参考米联miz702n)
Zynq的ARM通过GIC中断控制器来接收核仲裁所有的中断.由于中断向量表只有4Bytes大小,仅仅正好存放一条跳转语句,因此当产生一个外部中断时,中断处理的大致过程:PC内容保存到LR_IRQ用于中 ...
- 移动端为何不使用click而模拟tap事件及解决方案
移动端click会遇到2个问题,click会有200-300ms的延迟,同时click事件的延迟响应,会出现穿透,即点击会触发非当前层的点击事件. 为什么会存在延迟? Google开发者文档中有提到: ...
- 使用进程池规避Python的GIL限制
操作系统 : CentOS7.3.1611_x64 python版本:2.7.5 问题描述 Python的GIL会对CPU密集型的程序产生影响,如果完全使用Python来编程,怎么避开GIL的限制呢? ...
- PCA主成分分析
特征降维就是降低特征矩阵维数,减少噪声和冗余,减少过度拟合. Principal factor analysis简称PCA,其思想是将n维特征映射到k维上(k<n),这k维是全新的正交特征.这k ...
- sizeof求类的大小
用sizeof求类的大小,http://blog.csdn.net/szchtx/article/details/10254007(sizeof浅析(三)——求类的大小),这篇博文给出了非常详尽的举例 ...