SGU 232 Infinite Fraction Hash

题目链接:点击打开链接

#include <cstdio>
#include <cstring>
typedef unsigned long long ll;
const int key = (int)(1e9) + 7;

const int N = 150010;
char b[N], a[N + N];
ll xp[N], h[N + N];
int len;

void get() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int t = 0;
	b[t++] = ch;
	while ((ch = getchar()) >= '0' && ch <= '9')
		b[t++] = ch;
	b[t] = '\0';
}

void make(ll* h, char* s, int len) {
	h[len] = 0;
	for (int i = len - 1; i >= 0; --i)
		h[i] = h[i + 1] * key + (s[i] - '0' + 1);
}

ll H(ll *h, int st, int len) {
	return h[st] - h[st + len] * xp[len];
}
bool cmp(ll* h1, char* a1, int s1, ll* h2, char* a2, int s2) {
	if (a1[s1] != a2[s2])
		return a1[s1] > a2[s2];
	else if (H(h1, s1, len) == H(h2, s2, len))
		return true;

	int l = -1, r = len, mid;
	while (r - l > 1) {
		mid = (l + r) >> 1;
		if (H(h1, s1, mid) != H(h2, s2, mid))
			r = mid;
		else
			l = mid;
	}
	return a1[s1 + r - 1] > a2[s2 + r - 1];
}

int main() {
	xp[0] = 1;
	for (int i = 1; i < N; ++i)
		xp[i] = xp[i - 1] * key;

	int n, k, ansi, ansj, cc, pos;
	while(~scanf("%d%d", &n, &k)) {
		k %= n;
		//scanf("%s", b);
		get();
		if (k != 0 && n % k == 0)
			cc = k;
		else if (k == 0)
			cc = n;
		else
			cc = 1;
		len = n / cc;
		for (int i = 0; i < cc; ++i) {
			pos = i;
			for (int j = 0; j < len; ++j) {
				a[i * len * 2 + j] = a[i * len * 2 + j + len] = b[pos];
				pos = pos + k;
				if (pos >= n)
					pos -= n;
			}
			make(h + i * len * 2, a + i * len * 2, len * 2);
		}

		ansi = 0;
		ansj = 0;
		for (int i = 0; i < cc; ++i)
			for (int j = 0; j < len; ++j)
				if (cmp(h + i * len * 2, a + i * len * 2, j, h + ansi * len * 2, a + ansi * len * 2, ansj)) {
					ansi = i;
					ansj = j;
				}

		for (int j = 0; j < n; ++j)
			putchar(a[ansi * len * 2 + (ansj++) % len]);
		putchar('\n');
	}
	return 0;
}
时间: 2024-08-01 01:13:02

SGU 232 Infinite Fraction Hash的相关文章

HDU 6223 Infinite Fraction Path(BFS+剪枝)

The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly's talent and hoped

hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会得到一个长度为n的数列,输出能得到的最大的数列(当成数字). 思路: 一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs. 但是bfs肯定要剪枝,怎么剪枝呢? 1.按照思路,我要取每一位尽可能大的值,所以某一个状态的某

【HDOJ6223】Infinite Fraction Path(后缀数组)

题意: 给一个长度为n的字符串s[0..n-1],但i的后继不再是i+1,而是(i*i+1)%n,求所有长度为n的"子串"中,字典序最大的是谁 n<=150000,s[i]=0..9 思路:后缀数组 因为前驱与后继的关系已经变化,就不能用下标直接加减 i的后继是唯一的,i的前驱却不一定 所以对于后继使用倍增,对于前驱每个位置暴力开队列存储,需要的时候再拿出来 在判断的地方稍作修改 1 #include<cstdio> 2 #include<cstring>

ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path &amp;&amp; HDU 6223

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/details/78492841 注意优先队列自定义比较级的用法!! 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned long long 5 #define

hdu 6223 Infinite Fraction Path

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大.规则:从数字串S的某一个下标为x的数字出发,可以到达的下一个数字是下标为(x*x+1)%n的数字. 思路:BFS+剪枝.剪枝技巧: 1:遍历到某一层的节点时,记录已经到达过的节点,下次如果还经过就直接不考虑. 2:当前遍历到的某一层节点的数字较之前的小,直接不考虑. AC代码: #define _

HDU6223——2017ICPC徐州G Infinite Fraction Path

题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/9541442.html 思路: BFS, 一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs. 但是bfs肯定要剪枝,怎么剪枝呢? 1.按照思路,我要取每一位尽可能大的值,所以某一个状态的某一位小于我当前以及有的解,这个状态肯定

HDU6223 &amp;&amp; 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&amp;&amp;暴力

题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n)判定形成的字符串字典序是否比当前的最优解要大,复杂度O(n²),显然大家都会做. 而本题中有个结论:没有必要每次O(n),只要前100个字符一样,那么后面的一定都一样!所以>500直接break,复杂度O(500n), 可以过! 理解:对于所有的下标k,k向(k*k+1)%n连一条有向边,最后可以得

MemCache超详细解读

MemCache是什么 MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度.MemCaChe是一个存储键值对的HashMap,在内存中对任意的数据(比如字符串.对象等)所使用的key-value存储,数据可以来自数据库调用.API调用,或者页面渲染的结果.MemCache设计理念就是小而强大,它简单的设计促进了快速部署.易于开发并解决面对大规模的数据缓存的

StackExchange.Redis通用封装类分享

前两天朋友问我,有没有使用过StackExchange.Redis,问我要个封装类,由于之前都是使用ServiceStack.Redis,由于ServiceStack.Redis v4版本后是收费版的,所以现在也很有公司都在使用StackExchange.Redis而抛弃ServiceStack.Redis了.其实个人觉得,两个驱动都不错,只是由于ServiceStack.Redis收费导致目前很多公司都是基于V3版本的使用,也有人说V3版本有很多Bug,没有维护和升级,不过至少目前我是没发现B