poj3274
来源:互联网 发布:php入门看什么书 编辑:程序博客网 时间:2024/05/01 06:53
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define prime 9773
typedef struct A
{
int index;
struct A *next;
}A;
int distance = 0;
int map[100001][30]={0};
A *Hash[prime] = {0};
//K为牛特点的个数
void find(int cowCount,int K);
int cmp(int srci,int index,int K);
int getHash(int index,int K);
int getHash(int index,int K)
{
int j;
int hash = 1;
for(j=0; j<K; j++)
hash = hash*7 + map[index][j];
hash = (hash<0 ? -hash : hash) % prime;
return hash;
}
int cmp(int srci,int index,int K)
{
//比较srci与index中的内容
int i;
for(i=0; i<K && map[srci][i] == map[index][i]; i++)
;
return i==K;
}
void find(int cowCount,int K)
{
int i;
int hash;
A *p;
//注册
for(i=0; i<=cowCount; i++)
{
hash = getHash(i,K);
p = Hash[hash];
while(p)
{
//存在
if( cmp(i,p->index,K) )
{
//更新distance
if(i - (p->index) > distance)
{
distance = i - (p->index);
}
break;
}
p = p->next;
}
if(!p)
{
p = (A *)malloc(sizeof(A) * 1);
p->index = i;
p->next = Hash[hash];
Hash[hash] = p;
}
}
printf("%d\n",distance);
}
int main(void)
{
int tedian;
int i,j;
int N,K;//N牛数量,K特点的数目
memset(Hash,0,sizeof(A *)*prime);
memset(map,0,sizeof(int)*100000*30);
scanf("%d%d",&N,&K);
//初始化map
for(i=1; i<N+1; i++)
{
scanf("%d",&tedian);
for(j=0; j<K; j++)
map[i][j] = (tedian & (1<<j) ? 1 : 0);
}
//叠加
for(i=1; i<=N; i++)
for(j=0; j<K; j++)
map[i][j] = map[i][j] + map[i-1][j];
//减
for(i=1; i<=N; i++)
for(j=K-1; j>=0; j--)
map[i][j] = map[i][j]-map[i][0];
find(N,K);
}
#include<malloc.h>
#include<string.h>
#define prime 9773
typedef struct A
{
int index;
struct A *next;
}A;
int distance = 0;
int map[100001][30]={0};
A *Hash[prime] = {0};
//K为牛特点的个数
void find(int cowCount,int K);
int cmp(int srci,int index,int K);
int getHash(int index,int K);
int getHash(int index,int K)
{
int j;
int hash = 1;
for(j=0; j<K; j++)
hash = hash*7 + map[index][j];
hash = (hash<0 ? -hash : hash) % prime;
return hash;
}
int cmp(int srci,int index,int K)
{
//比较srci与index中的内容
int i;
for(i=0; i<K && map[srci][i] == map[index][i]; i++)
;
return i==K;
}
void find(int cowCount,int K)
{
int i;
int hash;
A *p;
//注册
for(i=0; i<=cowCount; i++)
{
hash = getHash(i,K);
p = Hash[hash];
while(p)
{
//存在
if( cmp(i,p->index,K) )
{
//更新distance
if(i - (p->index) > distance)
{
distance = i - (p->index);
}
break;
}
p = p->next;
}
if(!p)
{
p = (A *)malloc(sizeof(A) * 1);
p->index = i;
p->next = Hash[hash];
Hash[hash] = p;
}
}
printf("%d\n",distance);
}
int main(void)
{
int tedian;
int i,j;
int N,K;//N牛数量,K特点的数目
memset(Hash,0,sizeof(A *)*prime);
memset(map,0,sizeof(int)*100000*30);
scanf("%d%d",&N,&K);
//初始化map
for(i=1; i<N+1; i++)
{
scanf("%d",&tedian);
for(j=0; j<K; j++)
map[i][j] = (tedian & (1<<j) ? 1 : 0);
}
//叠加
for(i=1; i<=N; i++)
for(j=0; j<K; j++)
map[i][j] = map[i][j] + map[i-1][j];
//减
for(i=1; i<=N; i++)
for(j=K-1; j>=0; j--)
map[i][j] = map[i][j]-map[i][0];
find(N,K);
}
0 0
- POJ3274
- poj3274
- poj3274
- poj3274
- poj3274
- poj3274
- POJ3274
- poj3274
- poj3274 hash
- poj3274 hash
- Gold Balanced Lineup poj3274
- POJ3274 Gold Balanced Lineup
- poj3274 hash数组
- poj3274数位HASH
- poj3274 Gold Balanced Lineup
- [POJ3274]-Gold Balanced Lineup
- [POJ3274] Gold Balanced Lineup
- poj3274(数字hash)
- linux 重定向
- Storm命令详解
- ios判断是否为iphone6或iphone6plus代码
- ARM汇编-LDR指令学习
- STL-适配器-Stack
- poj3274
- Python标准库:内置函数staticmethod(function)
- “生动”讲解——矩阵的空间变换
- 读取配置文件 方式一
- caffe中安装python
- Android中Uri和ContentProvider以及ContentResolver之间的联系
- 9、Java反射(案例)
- poj3349
- 自测题IV——Have Fun with Numbers (20)