UVALive 4513 Stammering Aliens【Hash】

UVALive 4513 Stammering Aliens

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
#define met(s) memset(s, 0, sizeof(s))
#define rep(i, a, b) for(int i = a; i <= b; ++i)
template <class T> inline void scan_d(T &ret) {
char c; ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9') { 
ret = ret * 10 + (c - '0'), c = getchar();}}
typedef long long LL;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 4e4 + 10;
char str[MAXN];
ull has[MAXN], a[MAXN];
ull base = 233;
map<ull, int> mp;
int len, n;

void init_hash() {
	a[0] = 1;
	for(int i = 1; i < MAXN; ++i) a[i] = a[i - 1] * base;
	has[0] = 1;
	for(int i = 1; i <= len; ++i) {
		has[i] = has[i - 1] * base + str[i];
	}
}

bool judge(int x) {
	mp.clear();
	for(int i = 1; i <= len - x + 1; ++i) {
		ull ans = has[i + x - 1] - has[i - 1] * a[x];
		if(++mp[ans] >= n) return true;
	}
	return false;
}

int check(int x) {
	int t = 0; mp.clear();
	for(int i = 1; i <= len - x + 1; ++i) {
		ull ans = has[i + x - 1] - has[i - 1] * a[x];
		if(++mp[ans] >= n) t = i - 1;
	}
	return t;
}

int main() {
	while(scanf("%d", &n) && n) {
		scanf("%s", str + 1);
		int len = strlen(str + 1);
		for(int i = 0; i < len; ++i) {
			jfgjgj;
		}
		
		init_hash();
		int L = 0, R = len + 1;
		while(R - L >= 0) {
			int mid = (R + L) >> 1;
			if(judge(mid)) L = mid + 1;
			else R = mid - 1;
		}
		if(R == 0) printf("none\n");
		else {
			int t = check(R);
			printf("%d %d\n", R, t);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tingtingyuan/article/details/82011203