E. Locker Room 【后缀数组】 (2018-2019 CTU Open Contest)

任意门:http://codeforces.com/gym/101954/problem/E

E. Locker Room

time limit per test
2.0 s
memory limit per test
256 MB
input
standard input
output
standard output

There are several strange rooms in Binary Casino and one of them is a locker room. You have to enter the locker room several times a day, two times being a minimum (before and after your shift). There is no key or access code needed to enter the locker room. There is a brand new security lock system instead.

The lock system presents you with a generated puzzle which you have to solve every time you want to enter the locker room. This system prevents possible intruders to enter the locker room, as the puzzle takes them a long time to solve. Only employees, after working in the casino for some time, manage to master the puzzle.

It is your second week in the casino and you have already been late three times because you didn't manage to solve the puzzle quickly enough. You therefore decided to write a program which solves the puzzle. The puzzle is as follows:

You are given a cyclic string of NN lowercase English letters. You have to choose and mark substrings (continuous segments of characters) of a given length KK until each character in the string is marked. Marking a substring does not change the original string and each character can be marked multiple times. The task is to print the lexicographically maximal substring among chosen substrings. In addition, the printed substring has to be lexicographically minimal possible.

For example, let "acdb" be the given string of length N=4N=4 and let K=3K=3. Then you can choose substrings "acd" and "bac" to mark the whole string. The puzzle solution is "bac".

Input

The first line of input contains two integers NN and KK (1N51051≤N≤5⋅105, 1KN1≤K≤N), describing the length of the given string and the length of marked substrings. The second line contains NN lowercase English letters – the given cyclic string.

Output

Output the lexicographically maximal substring among chosen substrings under the condition the result is lexicographically minimal possible.

Examples
input
Copy
4 3
acdb
output
Copy
bac
input
Copy
6 2
aababa
output
Copy
ab
input
Copy
10 4
abaaabaaba
output
Copy
aaba
input
Copy
1 1
v
output
Copy
v

 题意概括:

在长度为 N 的主串中选取一个长度为 K 的子串可以把原主串全覆盖。

覆盖的条件是 选取的子串 s 把原串中所有长度为 K 并且字典序小于 s 的子串的字符标记之后(每个字符可被标记多次),

如果原串的全部字符都被标记,则当前选取的子串 s 可以覆盖这个主串。

求字典序最小的 s 。

原主串是一个环!!!

即最小化最大值。

解题思路:

模拟赛时想到的做法是 把所有长度为 K 的子串存进 set 然后枚举是否为这个答案,因为也是排好序的所以可以二分搜索,但判断条件依然是 O(N).结果在OJ上ML了

正解是后缀数组:

复制一遍原主串,因为是个环。

处理出后缀数组,然后二分后缀排名 t。

倍增版本 O(2*nlongn)

DC3版本  O(n+nlongn)

如何判断当前排名为 t 的后缀是可以的呢?

扫一遍原数组,遇到字典序排名大于 t 的后缀更新区间,

暴力是否有一段连续的区间长度大于等于 N,有则说明可以全覆盖。

如何统计这个区间呢?

定义一个起点 和 一个终点,如果遇到字典序比 s 大的后缀则跳过,否则更新终点指针。

如果当前枚举 的 i 已经大于 之前所覆盖的连续区间的终点 则更新 起点(即前面的连续区间已被打断)。

因为原主串是一个环,所以每次两点作差判断覆盖区间长度是否大于等于N。

如果当前排名 t 的后缀长度 小于 K,则它肯定无法覆盖原主串,因为相同字典序,长度长的字典序更大,暴力判断时,其所形成的连续区间必定小于 N;

如果当前排名 t 的后缀长度 大于等于 K,如何保证只取其长度为 K 的前缀作为答案呢?

这里很巧妙,统计区间时处理区间,总是 把终点更新为 当前小于 s (我们选取的后缀)的后缀起点 + K,也就是说只取前 K 个。

AC code:

  1 #include <bits/stdc++.h>
  2 #define inc(i, j, k) for(int i = j; i <= k; i++)
  3 #define rep(i, j, k) for(int i = j; i < k; i++)
  4 #define F(x) ((x)/3+((x)%3==1?0:tb))
  5 #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
  6 #define INF 0x3f3f3f3f
  7 #define LL long long
  8 using namespace std;
  9 const int maxn = int(3e6)+10;///注意空间是要开到 3N 的
 10 //const int N = maxn;
 11 
 12 #define F(x) ((x)/3+((x)%3==1?0:tb))
 13 #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
 14 int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];
 15 int r[maxn], sa[maxn], Rank[maxn], height[maxn];
 16 
 17 int c0(int *r,int a,int b)
 18 {return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}
 19 
 20 int c12(int k,int *r,int a,int b)
 21 {if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
 22 else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];}
 23 
 24 void Sort(int *r,int *a,int *b,int n,int m)
 25 {
 26     int i;
 27     for(i=0;i<n;i++) wv[i]=r[a[i]];
 28     for(i=0;i<m;i++) Ws[i]=0;
 29     for(i=0;i<n;i++) Ws[wv[i]]++;
 30     for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
 31     for(i=n-1;i>=0;i--) b[--Ws[wv[i]]]=a[i];
 32     return;
 33 }
 34 void dc3(int *r,int *sa,int n,int m) //涵义与DA 相同
 35 {
 36     int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
 37     r[n]=r[n+1]=0;
 38     for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;
 39     Sort(r+2,wa,wb,tbc,m);
 40     Sort(r+1,wb,wa,tbc,m);
 41     Sort(r,wa,wb,tbc,m);
 42     for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)
 43     rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
 44     if(p<tbc) dc3(rn,san,tbc,p);
 45     else for(i=0;i<tbc;i++) san[rn[i]]=i;
 46     for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;
 47     if(n%3==1) wb[ta++]=n-1;
 48     Sort(r,wb,wa,ta,m);
 49     for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
 50     for(i=0,j=0,p=0;i<ta && j<tbc;p++)
 51     sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
 52     for(;i<ta;p++) sa[p]=wa[i++];
 53     for(;j<tbc;p++) sa[p]=wb[j++];
 54     return;
 55 }
 56 
 57 void calheight(int *r, int *sa, int n)
 58 {
 59     int i, j, k = 0;
 60     for(i = 1; i <= n; i++) Rank[sa[i]] = i;
 61     for(i = 0; i < n; height[Rank[i++]] = k)
 62         for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++)
 63     return;
 64 }
 65 
 66 
 67 int N, K;
 68 string str;
 69 
 70 bool check(int t)
 71 {
 72     int b = 0, e = -1;
 73     for(int i = 0; i < N; i++){
 74         if(Rank[i] > t) continue;
 75         if(e < i) b = i;
 76         e = i+K;
 77         if(e-b >= N) return true;
 78     }
 79     return false;
 80 }
 81 
 82 int main()
 83 {
 84     scanf("%d %d", &N, &K);
 85     cin >> str;
 86     str+=str;
 87 //    cout << str << endl;
 88     int n_len = str.size();
 89     for(int i = 0; i < n_len; i++){
 90         r[i] = str[i]-'a'+1;
 91     }
 92 //    n_len--;
 93     r[n_len] = 0;
 94     dc3(r, sa, n_len+1, 30);
 95     calheight(r, sa, n_len);
 96 //    for(int i = 0; i < n_len; i++) cout << Rank[i] << endl;
 97 
 98     int L = 0, R = n_len, mid, ans;
 99     while(L <= R){
100         mid = (L+R)>>1;
101         if(check(mid)){
102             R = mid-1;
103             ans = mid;
104         }
105         else L = mid+1;
106     }
107 //    cout << ans << endl;
108     string res;
109     for(int i = 0; i < N; i++){
110         if(Rank[i] == ans){
111             res = str.substr(i, K);
112             cout << res << endl;
113             return 0;
114         }
115     }
116     return 0;
117 }
View Code

猜你喜欢

转载自www.cnblogs.com/ymzjj/p/10713972.html