题目链接: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. oracle触发器加条件判断、dblink

    --新增基站同步给电池组信息 create or replace trigger a_b_test after insert or update or delete on BJLT.BASESTATI ...

  2. error C2664: 'TextOutW' : cannot convert parameter 4 from const char [5]' to LPCTSTR

    转自:http://blog.sina.com.cn/s/blog_4aa4593d0100odra.html 问题的原因是字符串ANSI和Unicode编码的区别, VC6与VS2003等默认使用A ...

  3. Mysql 死锁的详细分析方法

    用数据库的时候,偶尔会出现死锁,针对我们的业务系统,出现死锁的直接结果就是系统卡顿.客户找事儿,所以我们也在想尽全力的消除掉数据库的死锁.出现死锁的时候,如果只是想解锁,用show full proc ...

  4. jquery获得select option的值 和对select option的操作

    jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change(function(){//code...});   //为Se ...

  5. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  6. NYOJ题目1045看美女

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAK5CAIAAADCdSR7AAAgAElEQVR4nO3dP3Lbuv434HcT7r2Q1F

  7. jQuery - 9.Ajax

    9.1 Ajax 的 XMLHttpRequest 对象 9.2 JQuery中的Ajax 9.2.1 load()方法 9.2.2 $.get() 9.2.3 $.post() 9.2.4 $.ge ...

  8. PHP define()的用法

    define()函数理解1(着重于作用的理解) define() 函数定义一个常量. 常量的特点: 常量类似变量,不同之处在于:在设定以后,常量的值无法更改常量名,不需要开头的美元符号 ($),作用域 ...

  9. 重温WCF之数据契约和序列化(四)

    一.数据契约 1.使用数据协定可以灵活控制哪些成员应该被客户端识别. [DataContract] public class Employee { [DataMember] public string ...

  10. 【翻译四】java-并发之线程暂停

    Pausing Execution with Sleep Thread.sleep causes the current thread to suspend execution for a speci ...