LA 6856 Circle of digits 解题报告
先用后缀数组给串排好序。dc3 O(n)
二分答案+贪心check
答案的长度len=(n+k-1)/k
如果起点为i长为len串大于当前枚举的答案,i的长度取len-1
从起点判断k个串的长度是否大于等于n
check的时候最多枚举len个起点,每个位置需要枚举n/len个串,时间复杂度O(n),总的时间复杂度O(nlogn+n)
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
#define ll long long
#define MAXN 1000009
int sa[MAXN], pos[MAXN], Rank[MAXN], r[MAXN];
int d, n, k;
string s;
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[MAXN], wb[MAXN], wv[MAXN], Ws[MAXN];
inline int c0 (int *r, int a, int b) {
return r[a] == r[b] && r[a + ] == r[b + ] && r[a + ] == r[b + ];
}
int c12 (int k, int *r, int a, int b) {
if (k == ) return r[a] < r[b] || r[a] == r[b] && c12 (, r, a + , b + );
else return r[a] < r[b] || r[a] == r[b] && wv[a + ] < wv[b + ];
}
inline void sort (int *r, int *a, int *b, int n, int m)
{
int i;
for (i = ; i < n; i++) wv[i] = r[a[i]];
for (i = ; i < m; i++) Ws[i] = ;
for (i = ; i < n; i++) Ws[wv[i]]++;
for (i = ; i < m; i++) Ws[i] += Ws[i - ];
for (i = n - ; i >= ; i--) b[--Ws[wv[i]]] = a[i];
return;
}
inline void dc3 (int *r, int *sa, int n, int m)
{
int i, j, *rn = r + n, *san = sa + n, ta = , tb = (n + ) / , tbc = , p;
r[n] = r[n + ] = ;
for (i = ; i < n; i++) if (i % != ) wa[tbc++] = i;
sort (r + , wa, wb, tbc, m);
sort (r + , wb, wa, tbc, m);
sort (r, wa, wb, tbc, m);
for (p = , rn[F (wb[])] = , i = ; i < tbc; i++)
rn[F (wb[i])] = c0 (r, wb[i - ], wb[i]) ? p - : p++;
if (p < tbc) dc3 (rn, san, tbc, p);
else for (i = ; i < tbc; i++) san[rn[i]] = i;
for (i = ; i < tbc; i++) if (san[i] < tb) wb[ta++] = san[i] * ;
if (n % == ) wb[ta++] = n - ;
sort (r, wb, wa, ta, m);
for (i = ; i < tbc; i++) wv[wb[i] = G (san[i])] = i;
for (i = , j = , p = ; i < ta && j < tbc; p++)
sa[p] = c12 (wb[j] % , r, wa[i], wb[j]) ? wa[i++] : wb[j++];
for (; i < ta; p++) sa[p] = wa[i++];
for (; j < tbc; p++) sa[p] = wb[j++];
return;
}
inline bool check (int x) {
for (int i = ; i < d; i++) {
int j = i, t = ;
while (t < k) {
if (Rank[j % n] <= x) j += d;
else
j += d - ;
t++;
}
if (j - i >= n) return ;
}
return ;
}
int main() {
while (scanf ("%d %d", &n, &k) == ) {
cin >> s; s += s;
d = (n + k - ) / k;
for (int i = ; i < (n << ); i++) r[i] = s[i] - '';
r[n << ] = ;
dc3 (r, sa, s.size() + , );
for (int i = , p = ; i <= (n << ); i++)
Rank[sa[i]] = ++p;
for (int i = , p = ; i <= (n << ); i++)
if (sa[i] < n ) pos[++p] = sa[i];
int l = , r = n, last = -;
while (l <= r) {
int mid = (l + r) >> ;
if (check (Rank[pos[mid]]) ) last = pos[mid], r = mid - ;
else
l = mid + ;
}
for (int i = last; i < last + d; i++)
putchar (s[i]);
putchar ();
}
}
LA 6856 Circle of digits 解题报告的更多相关文章
- BZOJ 3929 Circle of digits 解题报告
首先,我们可以得到最高位的位数为:\(\lfloor\frac{n+k-1}{n}\rfloor\),记作 \(E\). 然后给这 \(n\) 个长为 \(E\) 的数字排序,后缀数组 \(O((n+ ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- UVALive - 6856 Circle of digits 后缀数组+二分
题目链接: http://acm.hust.edu.cn/vjudge/problem/82135 Circle of digits Time Limit: 3000MS 题意 把循环串分割成k块,让 ...
- 【LeetCode】 258. Add Digits 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:减1模9 方法三:直接模9 日 ...
- 【LeetCode】949. Largest Time for Given Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】357. Count Numbers with Unique Digits 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 788 Rotated Digits 解题报告
题目要求 X is a good number if after rotating each digit individually by 180 degrees, we get a valid num ...
- LeetCode 258 Add Digits 解题报告
题目要求 Given a non-negative integer num, repeatedly add all its digits until the result has only one d ...
随机推荐
- eclipse中 com.sun.image.codec.jpeg.JPEGCodec 无法编译通过问题
在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEG ...
- 配置Myeclipse中的项目部署到服务器,报the selected server is enabled, but is not configured properly.
the selected server is enabled, but is not configured properly. deployment to it will not be permitt ...
- HDU-1963
http://acm.hdu.edu.cn/showproblem.php?pid=1963 完全背包. 题意:给出初始资金,还有年数,然后给出每个物品的购买价格与每年获得的利益,要求在给出的年份后所 ...
- python for selenium 数据驱动测试
# -*- coding:utf-8 -*- """ 数据驱动测试,从 csv 文件中读取数据 """ from selenium impo ...
- 新手入门 acm 输入输出练习
A + B Problem(1000) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- asp json
<script language="JScript" runat="Server">function toObject(json) { eva ...
- TCP HTTP 详细内存分析 & time_wait setsockopt
http://www.kegel.com/c10k.html#nb.edge http://www.chinasb.org/archives/2012/11/4954.shtml UDP协议:发送进程 ...
- LOL游戏程序中对一些函数的Hook记录(Win10 x64)
[PC Hunter Standard][League of Legends.exe-->Ring3 Hook]: 108Hooked Object Hook Address and Locat ...
- [2013 ACM/ICPC Asia Regional Nanjing Online C][hdu 4750]Count The Pairs(kruskal + 二分)
http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意: 定义f(u,v)为u到v每条路径上的最大边的最小值..现在有一些询问..问f(u,v)>=t ...
- Java Struts2 的请求处理流程详解
一.Struts2的处理流程: 客户端产生一个HttpServletRequest的请求,该请求被提交到一系列的标准过滤器(Filter)组建链中(如ActionContextCleanUp:它主要是 ...