[whu1564]后缀数组
http://acm.whu.edu.cn/land/problem/detail?problem_id=1564
思路:先把串复制一遍,在末尾补个标记,后缀数组跑一下,扫一遍就ok了(过滤后缀在后半部分的)。
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl typedef double db;
typedef long long LL;
typedef pair<int, int> pii;
typedef multiset<int> msi;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e6 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } /// 构造后缀数组的之前,需要在原串末尾加个空字符(比其它字符都小即可),
///把这个空字符看成原串的一部分(这样在比较的时候到这个位置一定可以分个大小),
///所以n应该为原序列长度+1,后缀n-1是"空串",sa[0]总是n-1。
struct SuffixArray {
int n;
int arr[][maxn];
int *sa, *x, *y, *c, *rnk, *height;
SuffixArray() { sa = arr[]; x = arr[]; y = arr[]; c = arr[]; rnk = arr[]; height = arr[]; }
void resize(int nn) { n = nn; mem0(arr[]); }
void build_sa(int s[], int m) { // m is biger than the max value of char
rep_up0(i, m) c[i] = ;
rep_up0(i, n) c[x[i] = s[i]]++;
rep_up1(i, m - ) c[i] += c[i - ];
rep_down0(i, n) sa[--c[x[i]]] = i;
for (int k = ; k <= n; k <<= ) {
int p = ;
for (int i = n - k; i < n; i++) y[p++] = i;
rep_up0(i, n) if (sa[i] >= k) y[p++] = sa[i] - k;
rep_up0(i, m) c[i] = ;
rep_up0(i, n) c[x[y[i]]]++;
rep_up0(i, m) c[i] += c[i - ];
rep_down0(i, n) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for (int i = ; i < n; i++) {
x[sa[i]] = y[sa[i - ]] == y[sa[i]] && y[sa[i - ] + k] == y[sa[i] + k]? p - : p++;
}
if (p >= n) break;
m = p;
}
}
void build_height(int s[]) {
int k = ;
rep_up0(i, n) rnk[sa[i]] = i;
rep_up0(i, n) {
if (k) k--;
int j = sa[rnk[i] - ];
while (s[i + k] == s[j + k]) k++;
height[rnk[i]] = k;
}
}
}; SuffixArray sa;
char s[maxn];
int ss[maxn]; int main() {
//freopen("in.txt", "r", stdin);
int n, k;
while (cin >> n >> k) {
scanf("%s", s);
rep_up0(i, n) ss[i + n] = ss[i] = s[i];
ss[ * n] = ;
int len = n * + ;
sa.resize(len);
sa.build_sa(ss, );
int cnt = ;
for (int i = ; i < len; i++) {
if (sa.sa[i] < n) {
cnt++;
if (cnt == k) {
int x = sa.sa[i];
if (x == ) x = n;
cout << x << endl;
break;
}
}
}
}
return ;
}
[whu1564]后缀数组的更多相关文章
- 后缀数组的倍增算法(Prefix Doubling)
后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- 后缀数组(suffix array)详解
写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具. 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料. 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, ...
- 【UOJ #35】后缀排序 后缀数组模板
http://uoj.ac/problem/35 以前做后缀数组的题直接粘模板...现在重新写一下模板 注意用来基数排序的数组一定要开到N. #include<cstdio> #inclu ...
- 【BZOJ-2119】股市的预测 后缀数组
2119: 股市的预测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 334 Solved: 154[Submit][Status][Discuss ...
- 【BZOJ-4698】Sandy的卡片 后缀数组
4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 140 Solved: 55[Submit][Stat ...
随机推荐
- api测试用例(编写思路)
在API的自动化测试维度中,测试维度分为两个维度,一个是单独的对API的验证,客户端发送一个请求后,服务端得到客户端的请求并且响应回复给客户端: 另外一个维度是基于业务场景的测试,基于业务场景的也就是 ...
- P1464 Function
Function 简 单 的 递 归 这道题一开始十分智障地用递归做,虽然知道没那么简单,但还是冒着送死的心态交了一遍,果然,如我所料 样例输入: 密密麻麻,几万行的样例输入 //:) ...
- Linux 高 wio 分析
High IO wait Table of Contents 1. 现象 2. 分析 2.1. iotop或者pidstat 2.1.1. iotop 2.1.2. pidstat 2.2. 脚本 2 ...
- 关于搭建IIS网页弹出登录框的解决方案
今天自己搭建IIS服务器的时候,明明设置了匿名访问,但是用ie访问127.0.0.1的时候还是会弹出一个登陆框,最后在网上找到答案. 转自: https://blog.csdn.net/sunleib ...
- Python(10)
如果 a+b+c=1000,且 a^2+b^2=c^2(a,b,c 为自然数),如何求出所有a.b.c可能的组合? # 注意是三重循环 for a in range(0, 1001): for b i ...
- python学习02python入门二
学前须知:1.本文档有关内容均建立在python3.x版本上,python2.x已经成为历史,如有需要,文内会特别说明. 2.本文使用的编辑器多为架构在Windows上的pycharm,如需了解Lin ...
- 痞子衡嵌入式:揭秘i.MXRT1170 eFuse空间访问可靠性的保护策略(冗余与ECC)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦i.MXRT1170的eFuse空间访问可靠性保护策略. 关于i.MXRT系列的eFuse/OTP,痞子衡之前在介绍Boot时写过 ...
- MYSQL 索引汇总
1.MySQL索引类型 先分以下类,MYQL有两大类索引:聚集索引和非聚集索引(只考虑mysql innodb) 聚集索引:在有主键的情况下,主键为聚集索引,其他都是非聚集索引 ...
- c++ 如何开N次方?速解
c++ 如何开N次方?速解 直接上代码 #include <iostream> #include <cmath> using namespace std; typedef ...
- while循环脚本
[root@oldboy ~]# (while :;do date;sleep 5;done)& fg ctrl c退出 fg ( while :; do date; sleep 5; don ...