Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

转化 + 开散列

Posted by zhouzp15 at 2017-01-19 23:01:33 on Problem 3274
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;

const int N = 100000 + 7;
const int M = 30 + 1;
int n, m;
int sum[N][M], delta[N][M], ht[N], table[M];

inline int hash(int idx)
{
	int key = 0;
	for (int i = 1; i <= m; i++) key = key * 10 + delta[idx][i];
	return abs(key) % N;
}

inline bool check(int idx1, int idx2)
{
	for (int i = 1; i <= m; i++) if (delta[idx1][i] != delta[idx2][i]) return false;
	return true;
}

inline int insert(int idx)
{
	int key = hash(idx);
	while (ht[key] != -1) 
	{
		if ( check(idx, ht[key]) ) return idx - ht[key];
		key = (key + 1) % N;
	}
	ht[key] = idx;
	return 0;
}

int main()
{
	int maxLen = 0, tmpLen;
	for (int i = 1; i <= N; i++) ht[i] = -1;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++) table[i] = 1 << (i - 1);
	for (int i = 1; i <= n; i++)
	{
		int tmp;
		scanf("%d", &tmp);
		for (int j = 1; j <= m; j++) sum[i][j] = sum[i - 1][j] + (tmp & 1), tmp >>= 1, delta[i][j] = sum[i][j] - sum[i][1];
	}
	for (int i = 1; i <= n; i++) if ( (tmpLen = insert(i)) > maxLen ) maxLen = tmpLen;
	printf("%d\n", maxLen);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator