题目链接: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的更多相关文章

  1. Gold Balanced Lineup - poj 3274 (hash)

    这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum ...

  2. poj 3274 Gold Balanced Lineup(哈希 )

    题目:http://poj.org/problem?id=3274 #include <iostream> #include<cstdio> #include<cstri ...

  3. POJ 3274 Gold Balanced Lineup(哈希)

    http://poj.org/problem?id=3274 题意 :农夫约翰的n(1 <= N <= 100000)头奶牛,有很多相同之处,约翰已经将每一头奶牛的不同之处,归纳成了K种特 ...

  4. POJ 3349 HASH

    题目链接:http://poj.org/problem?id=3349 题意:你可能听说话世界上没有两片相同的雪花,我们定义一个雪花有6个瓣,如果存在有2个雪花相同[雪花是环形的,所以相同可以是旋转过 ...

  5. POJ 1840 HASH

    题目链接:http://poj.org/problem?id=1840 题意:公式a1x1^3+ a2x2^3+ a3x3^3+ a4x4^3+ a5x5^3=0,现在给定a1~a5,求有多少个(x1 ...

  6. POJ 2785 HASH

    题目链接:http://poj.org/problem?id=2785 题意:给定n行数字,每行4个数分别是a,b,c,d,现在要求能有多少个(a,b,c,d)组合并且和为0 思路:n^2统计所有(a ...

  7. 哈希 poj 3274

    n个牛 二进制最多k位 给你n个数 求max(j-i)&&对应二进制位的和相同 7    1  1  1  倒的 6    0  1  1 7    1  1  1 2    0  1 ...

  8. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  9. 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 ...

随机推荐

  1. 【leetcode】Binary Tree Preorder Traversal (middle)★

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  2. 2048控制台程序:一份帝国理工C++作业

    #include <fstream> #include <vector> #include <iostream> #include <string> u ...

  3. [Android Pro] 使用apktool工具遇到could not decode arsc file的解决办法

    转:http://www.cnblogs.com/sage-blog/p/4323049.html 最近使用APKtool工具反编译APK老是提示不成功,错误如下: Exception in thre ...

  4. java.util.ConcurrentModificationException

    遍历 List 的时候将 item remove 掉会抛出此异常

  5. NYOJ题目806HEIHEI的心情

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtMAAANRCAIAAACumacBAAAgAElEQVR4nOzdrXLrOrjG8XMT5b2Q4l

  6. 浏览器方法及代码打包成APP的

    <script src=" http://static.ydbimg.com/API/YdbOnline.js" type="text/javascript&quo ...

  7. hdu 2602

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 #include<cstdio> #include<iostream> ...

  8. 《CLR via C#》读书笔记(5)基元类型、引用类型和值类型

    5.1 基元类型 编译器直接支持的数据类型称为基元类型(primitive type). 以下4行到吗生成完全相同的IL int a = 0; //最方便的语法 System.Int32 b = 0; ...

  9. C语言中do...while(0)的妙用

    在linux内核代码中,经常看到do...while(0)的宏,do...while(0)有很多作用,下面举出几个: 1.避免goto语句: 通常,如果一个函数开始要分配一些资源,然后如果在中途遇到错 ...

  10. Delphi测试线程的时间

    在16位时代,当我们在Windows3.x下编程时,经常会用到GetTickCount()或者timeGetTime()来判断一段代码的执行时间.示例如下 var StartTime, Total: ...