2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821
解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比赛中的一个求平方和的题目思路很类似。
首先说一下hash,简单来说就是y = hash(x),有很多函数,可以参考这里:https://www.byvoid.com/blog/string-hash-compare/
然后,我用的是这个:写法简单,而且重复的可能较小。
// BKDR Hash Function
unsigned int BKDRHash(char *str)
{
unsigned int seed = ; // 31 131 1313 13131 131313 etc..
unsigned int hash = ; while (*str)
{
hash = hash * seed + (*str++);
} return (hash & 0x7FFFFFFF);
}
然后回到这个题目:
3W,4RE,3TLE,首先wa的原因:循环次数少了一,<=slen,超时没办法,只能优化了。
下面是没有优化的代码,每个子串求一下hash.
/*
#include <cstdio>
#include <cstring>
#include <string> using namespace std; const int inf = 2000000; bool t[2000000];
int Hash[100005]; long long myhash(char *str)
{
int seed = 31;
long long hash = 0;
while (*str)
hash =( hash * seed + (*str++)-'a' + 1) %inf ;
return hash;
} int main()
{
int m,l;
while(scanf("%d%d",&m,&l)!=EOF)
{
char str[100005];
scanf("%s",str);
int len = strlen(str);
int ans = 0; int k = 0;
for(int i=0; i+m<len; i++)
{
char temp[100005];
for(int j=0; j<l; j++)
temp[j] = str[i+k*m+j];
temp[l] = '\0';
long long hashs = myhash(temp);
Hash[i+k*m] = myhash()
} for(int i=0; i<l&&i+m*l<=len; i++)
{
memset(t,false,sizeof(t));
bool flag = true;
for(int k = 0; k<m; k++)
{
char temp[100005];
for(int j=0; j<l; j++)
temp[j] = str[i+k*m+j];
temp[l] = '\0';
long long hashs = myhash(temp);
if(t[hashs])
{
flag = false;
break;
}
t[hashs] = true;
}
if(flag) ans++;
}
printf("%d\n",ans);
}
return 0;
} */
优化方案。上图比较好!!!
优化的位置就是成段删掉,成段添加。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map> using namespace std;
const int MAXN = ;
const unsigned long long base = ; unsigned long long nbase[MAXN],Hash[MAXN];
int n,len,ans,slen;
char str[MAXN];
map<unsigned long long, int> mp; int main()
{
unsigned long long tmp;
nbase[] = ;
for (int i = ; i <= MAXN; i++)
nbase[i] = nbase[i-] * base; while (scanf("%d%d", &n, &len) != EOF)
{
scanf("%s", str);
slen = strlen(str);
Hash[slen] = ;
for (int i = slen-; i >= ; i--)
Hash[i] = Hash[i+]*base+str[i]-'a'+; ans = ;
for (int i = ; i < len && i+n*len <= slen; i++)
{
mp.clear();
for (int j = i; j < i+n*len; j += len)
{
tmp = Hash[j] - Hash[j+len]*nbase[len];
mp[tmp]++;
}
if (mp.size() == n)
ans++;
for (int j = i+n*len; j+len <= slen; j += len)
{
tmp = Hash[j-n*len] - Hash[j-(n-)*len]*nbase[len];
mp[tmp]--;
if (mp[tmp] == )
mp.erase(tmp);
tmp = Hash[j] - Hash[j+len]*nbase[len];
mp[tmp]++;
if (mp.size() == n)
ans++;
}
}
printf("%d\n", ans);
}
return ;
}
2013 Asia Regional Changchun I 题,HDU(4821),Hash的更多相关文章
- 2013 Asia Regional Changchun C
Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K ( ...
- HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spheric ...
- HDU 4822 Tri-war(LCA树上倍增)(2013 Asia Regional Changchun)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow ...
- 2013 Asia Regional Changchun
Hard Code http://acm.hdu.edu.cn/showproblem.php?pid=4813 #include<cstdio> ]; int main(){ int t ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...
- (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)
http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others) ...
- 2015ACM/ICPC Asia Regional Changchun Online /HDU 5438 图
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 1310 ...
- hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...
随机推荐
- c++ unique_lock lock_guard
unique_lock template <class Mutex> class unique_lock; Unique lock A unique lock is an object t ...
- Swift实战-豆瓣电台(四)歌曲列表的展现
观看地址 http://v.youku.com/v_show/id_XNzMwNDE0OTA4.html 这节的主要内容是如何利用cell展现获取到的数据. 首先申明两个数组来储存我们获取到的数据 v ...
- Python学习总结3:元组、列表的操作汇总
参考博客:http://www.cnblogs.com/QG-whz/p/4782809.html 1. 是否可变 元组:用()或tuple函数定义,不可变(元素的值以及整个元组): 列表:用 [] ...
- ligerui_实际项目_001:利用ligerLayout、ligerAccordion实现可折叠的菜单效果
效果:利用ligerLayout.ligerAccordion实现可折叠的菜单效果 可能用到的js.css.images等,可到官网下载: 第01步:引入相应的文件 <head><l ...
- hue安装与部署
运行环境 centOS 6.6 hadoop 2.4.0 hive 1.2.0 spark 1.4.1 HUE 3.9 介绍: Hue是一个开源的Apache Hadoop UI系统,最早是由Clou ...
- C# 实现 单例模式
http://blog.sina.com.cn/s/blog_75247c770100yxpb.html
- js 获取选中的多选框
前台html <span class="spbox"> <input type="checkbox" name="category& ...
- clock
Prime Time中的clock分析包括: 1)Multiple clocks,clock from port/pin,virtual clock. 2)Clock network delay an ...
- c语言 typedef
在C和C++编程语言中,typedef是一个关键字.它用来对一个资料类型取一个新名字.目的是为了使源代码更易于阅读和理解. 来看以下程式码: int coxes; int jaffa; ... c ...
- dataTabel转成dataview插入列后排序
if (!string.IsNullOrEmpty(strQuyu) && !string.IsNullOrEmpty(strZuhao)) { string[] param = { ...