POJ 3274 HASH
题目链接:http://poj.org/problem?id=3274
题意+思路: 点击这里
补充:因为有减法运算,所以可能会造成运算后结果为负数,所以需要把结果统一转换成正数[不然数组下标访问不到负数位置],还有要先把0放入哈希表,不然会出现长度为1但是没有匹配的情况
比如:1 3
7
结果为1,如果没把0放进哈希表则结果为0
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<time.h>
using namespace std;
typedef long long int LL;
const int MAXN=+;
const int MAXK=+;
const int MOD=;
int Num[MAXN][MAXK]; //data storage
struct Node{
int id,Next;
}Cow[MAXN];
int n,k,S[MAXN][MAXK];
int Scnt,Head[MOD];//hash table
void Init(){ //initialization
memset(Head,-,sizeof(Head));
memset(S,,sizeof(S));
Scnt=;
}
void AddNode(int HashN,int idx){
Cow[Scnt].id=idx;
Cow[Scnt].Next=Head[HashN];
Head[HashN]=Scnt++;
}
bool cmp(int idx,int idy){ //compare two array
for(int i=;i<k;i++){
if(S[idx][i]!=S[idy][i]){
return false;
}
}
return true;
}
int solve(){
int ans=;
for(int i=;i<=n;i++){ //insert 0 into the hash table first!
int HashNum=; //i==0 then insert 0 into the hash table because Data from 1 to n
for(int j=;j<k;j++){
HashNum=((HashNum+S[i][j])+MOD)%MOD;
}
for(int j=Head[HashNum];j!=-;j=Cow[j].Next){
if(cmp(i,j)){
ans=max(ans,i-j);
}
}
AddNode(HashNum,i);
}
return ans;
}
void make_S(){//make array S
for(int i=;i<=n;i++){
for(int j=;j<k;j++){
S[i][j]=S[i-][j]+Num[i][j];
}
}
}
void make_C(){ //make array C
for(int j=k-;j>=;j--){
for(int i=;i<=n;i++){
S[i][j]-=S[i][];
}
}
}
int main(){
#ifdef kirito
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int start=clock();
while(~scanf("%d%d",&n,&k)){
Init();
for(int i=;i<=n;i++){
int val;
scanf("%d",&val);
for(int j=;j<k;j++,val/=){
Num[i][j]=val%;
}
}
make_S(); make_C();
printf("%d\n",solve());
}
#ifdef LOCAL_TIME
cout << "[Finished in " << clock() - start << " ms]" << endl;
#endif
return ;
}
POJ 3274 HASH的更多相关文章
- Gold Balanced Lineup - poj 3274 (hash)
这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...
- poj 3274 Gold Balanced Lineup(哈希 )
题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...
- POJ 3274 Gold Balanced Lineup(哈希)
http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...
- POJ 3349 HASH
题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...
- POJ 1840 HASH
题目链接:http://poj.org/problem?id=1840 题意:公式a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0,现在给定a1~a5,求有多少个(x1 ...
- POJ 2785 HASH
题目链接:http://poj.org/problem?id=2785 题意:给定n行数字,每行4个数分别是a,b,c,d,现在要求能有多少个(a,b,c,d)组合并且和为0 思路:n^2统计所有(a ...
- 哈希 poj 3274
n个牛 二进制最多k位 给你n个数 求max(j-i)&&对应二进制位的和相同 7 1 1 1 倒的 6 0 1 1 7 1 1 1 2 0 1 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3
Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...
随机推荐
- Android 开发技巧 - Android 6.0 以上权限大坑和权限检查基类封装
简单介绍 关于运行时权限的说法,早在Google发布android 6.0的时候,大家也听得蛮多的.从用户的角度来讲,用户是受益方,更好的保护用户的意思,而对于开发者来说,无疑增加了工作量. 对于6. ...
- LeetCode 459 Repeated Substring Pattern
Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...
- 一次工作中用到的Highcharts.Chart
一般动态获取图表信息都是通过ajax交互传送数据. 这次是一次性从后台返回集合后,直接在页面取数据绘制图表 引用js <script type="text/javascript&quo ...
- MyEclipse在线安装maven插件最新地址
http://repository.sonatype.org/content/sites/forge-sites/m2e/0.10.0/S/20100209-0800/
- Qt5_简易画板_详细注释
代码下载链接: http://pan.baidu.com/s/1hsc41Ek 密码: 5hdg 显示效果如下: 代码附有详细注释(代码如下) /*** * 先新建QMainWindow, 项目名称 ...
- CSS3与页面布局学习总结——Box Model、边距折叠、内联与块标签、CSSReset
目录 一.盒子模型(Box Model) 1.1.宽度测试 1.2.溢出测试 1.3.box-sizing属性 1.4.利用CSS画图 二.边距折叠 2.1.概要 2.2.垂直方向外边距合并计算 三. ...
- js获取缓存数据
后台:request.setAttribute("type", type); 前台js获取:var type = "${type}";
- C/C++学习笔记----指针的理解
指针是C/C++编程中的重要概念之一,也是最容易产生困惑并导致程序出错的问题之一.利用指针编程可以表示各种数据结构,通过指针可使用主调函数和被调函数之间共享变量或数据结构,便于实现双向数据通讯:指针能 ...
- Delphi中的异常处理
转载:http://www.cnblogs.com/doit8791/archive/2012/05/08/2489471.html 以前写Delphi程序一直不注意异常处理,对其异常处理的机制总是一 ...
- Yii 同域名的单点登录 SSO实现
SSO (Single Sign-on) 顾名思义就是几个子项目共用一个登录点. 原理简单来说就是服务端session 共享, 客户端跨域cookies. 实现非常简单,protected/confi ...