Alice's Classified Message
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 33 Accepted Submission(s): 11
Problem Description
Alice wants to send a classified message to Bob. She tries to encrypt the message with her original encryption method. The message is a string
S, which consists of
N lowercase letters.
S[a…b] means a substring of S ranging from
S[a] to
S[b] (
0≤a≤b<N). If the first
i letters have been encrypted, Alice will try to find a magic string
P. Assuming
P has
K letters,
P is the longest string which satisfies
P=S[T...T+K−1] (
0≤T<i,
T+K≤N) and
P=S[i…i+K−1](i+K≤N). In other words,
P is a substring of
S, of which starting address is within
[0...i−1], and
P is also a prefix of
S[i...N−1]. If
P exists, Alice will append integer
K and
T to ciphertext. If
T is not unique, Alice would select the minimal one. And then
i is incremented by
K. If
P does not exist, Alice will append -1 and the ASCII code of letter
S[i] to ciphertext, and then increment
i by 1.
Obviously the first letter cannot be encrypted. That is to say,
P does not exist when
i=0. So the first integer of ciphertext must be -1, and the second integer is the ASCII code of
S[0].
When
i=N, all letters are encrypted, and Alice gets the final ciphertext, which consists of many pairs of integers. Please help Alice to implement this method.
Input
The first line of input contains an integer
T, which represents the number of test cases (
T≤50). Each test case contains a line of string, which has no more than 100000 lowercase letters. It is guaranteed that the total length of the strings is not greater than
2×106.
Output
For each test case, output a single line consisting of “
Case #X:” first.
X is the test case number starting from 1. Output the ciphertext in the following lines. Each line contains two integers separated by a single space.
Sample Input
2 aaaaaa aaaaabbbbbaaabbc
Sample Output
Case #1: -1 97 5 0 Case #2: -1 97 4 0 -1 98 4 5 5 2 -1 99
Source
2015ACM/ICPC亚洲区合肥站-重现赛(感谢中科大)
Recommend
wange2014 | We have carefully selected several similar problems for you: 5566 5565 5564 5563 5562
这题子串与子串相匹配,八成是后缀数组,那么我们先求出height数组,但题目要求一个串必须从i开始,另一个串必须在i之前开始,那么,我们就可以在后缀排序之后的字符串集中,从i串向前二分查找最近的在i串之前的串,从i串向后二分查找最近的在i串之前的串,那么我们就知道一个答案了,另一个答案要求序号最小,我们可以向前向后二分查找满足答案的最远串,最后求两个最远串之间的序号最小的串,复杂度O(nlogn)常数较大,不知道排名第一的代码怎么写的。
#include
#include
#include
#include
#include
#include
using namespace std;
int n;
const int maxn=110000;
struct Suffix{
Suffix(){}
char s[maxn];int rk[maxn],h[maxn],c[maxn],t[maxn],sa[maxn];
void build(){
int *x=t,*sa2=rk,m=200;
for(int i=0;i=0;i--)sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1){
int p=0;
for(int i=n-k;i=k)sa2[p++]=sa[i]-k;
for(int i=0;i=0;i--)sa[--c[x[sa2[i]]]]=sa2[i];
swap(x,sa2);
p=1;x[sa[0]]=0;
for(int i=1;i=n)break;
m=p;
}
}
int rank[maxn];
void calheignt(){
int k=0;
for(int i=0;i=0;i--){
_st[i][0]=h[i];n_st[i][0]=sa[i];
for(int j=1,k=1;2*j+i-1>1;
if(S.n_st_query(mid,x-1)>1;
if(S.n_st_query(x+1,mid)>1;
if(S.st_query(mid+1,x)>1;
if(S.st_query(x+1,mid)0)x=S.st_query(l+1,i);
int r=queryr(i);
int y=0;
if(rx){
int c=queryrr(i,y);
int z=S.n_st_query(i,c);
printf("%d %d\n",y,z);
i=S.sa[i];i+=y;
}else if(y