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=Si…i+K−1. 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
题意:给出一个字符串,可以每次可以走前面的点和它最长相同字串的长度,在i位置时,取j
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+7;
char st[N];
struct SAM{
int pre[20*N],nex[20*N][26],mx[20*N],last,top;
int ed[2*N];
void init(){
top = 2;
last = 1;
memset(nex[1],0,sizeof(nex[1]));
}
int newnode(int x){
//cout <<"!!!" << top << endl;
mx[top] = x;
for(int i = 0;i < 26;i ++) nex[top][i] = 0;
return top ++;
}
void add(int pos,int x){
int np = newnode(mx[last]+1);
ed[np] = pos;
int p = last;
while(p && nex[p][x] == 0) {
nex[p][x] = np;
//ed[np] = min(ed[np],ed[last]+1);
p = pre[p];
}
if(!p){
pre[np] = 1;
}
else{
int q = nex[p][x];
if(mx[q] == mx[p]+1){
pre[np] = q;
}
else {
int nq = newnode(mx[p]+1);
ed[nq] = ed[q];
for(int i = 0;i < 26;i ++) nex[nq][i] = nex[q][i];
pre[nq] = pre[q];
pre[np] = pre[q] = nq;
while(p && nex[p][x] == q){
//ed[nq] = min(ed[nq],last+1);
nex[p][x] = nq;
p = pre[p];
}
}
}
last = np;
}
int get(int x,int y){
return nex[x][y];
}
}sam;
int main(){
int T;
cin >> T;
for(int kase = 1;kase <= T;kase ++){
scanf("%s",st);
printf("Case #%d:\n",kase);
sam.init();
int len =strlen(st);
int now = 0;
while(now < len){
int nex = now;
int pos = 1;
while(nex < len){
if(sam.get(pos,st[nex]-'a')) {
sam.add(nex,st[nex]-'a');
pos = sam.get(pos,st[nex]-'a');
nex ++;
}
else break;
}
if(nex != now){
printf("%d %d\n",nex-now,sam.ed[pos]-(nex-now-1));
now = nex;
}
else{
printf("-1 %d\n",st[now]);
sam.add(now,st[now]-'a');
now ++;
}
}
}
return 0;
}