Alice's Classified Message
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 312 Accepted Submission(s): 122
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.
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
1 /* 2 hdu5558 后缀数组 3 4 从[1,n]对于每个i,求suff[j](j < i)与suff[i]的最长公共前缀, 5 如果有多个,取最小的那个 6 7 我们可以通过后缀数组先求出,如果i-1和i,i和i+1都有公共前缀, 8 那么i-1和i+1也有公共前缀,所以可以先处理出每个i的左右界限。然后对于i左右扫描一下即可 9 10 然后枚举i,从pre[i]-nex[i]找到合适的结果即可 11 12 hhh-2016-03-10 18:17:04 13 */ 14 #include <algorithm> 15 #include <cmath> 16 #include <queue> 17 #include <iostream> 18 #include <cstring> 19 #include <map> 20 #include <cstdio> 21 #include <vector> 22 #include <functional> 23 #define lson (i<<1) 24 #define rson ((i<<1)|1) 25 using namespace std; 26 typedef long long ll; 27 const int maxn = 150050; 28 29 int t1[maxn],t2[maxn],c[maxn]; 30 bool cmp(int *r,int a,int b,int l) 31 { 32 return r[a]==r[b] &&r[l+a] == r[l+b]; 33 } 34 35 void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m) 36 { 37 n++; 38 int p,*x=t1,*y=t2; 39 for(int i = 0; i < m; i++) c[i] = 0; 40 for(int i = 0; i < n; i++) c[x[i] = str[i]]++; 41 for(int i = 1; i < m; i++) c[i] += c[i-1]; 42 for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i; 43 for(int j = 1; j <= n; j <<= 1) 44 { 45 p = 0; 46 for(int i = n-j; i < n; i++) y[p++] = i; 47 for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j; 48 for(int i = 0; i < m; i++) c[i] = 0; 49 for(int i = 0; i < n; i++) c[x[y[i]]]++ ; 50 for(int i = 1; i < m; i++) c[i] += c[i-1]; 51 for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; 52 53 swap(x,y); 54 p = 1; 55 x[sa[0]] = 0; 56 for(int i = 1; i < n; i++) 57 x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++; 58 if(p >= n) break; 59 m = p; 60 } 61 int k = 0; 62 n--; 63 for(int i = 0; i <= n; i++) 64 Rank[sa[i]] = i; 65 for(int i = 0; i < n; i++) 66 { 67 if(k) k--; 68 int j = sa[Rank[i]-1]; 69 while(str[i+k] == str[j+k]) k++; 70 height[Rank[i]] = k; 71 } 72 } 73 74 int pre[maxn],nex[maxn]; 75 int Rank[maxn],height[maxn]; 76 int sa[maxn],str[maxn]; 77 char a[maxn]; 78 int len; 79 80 int main() 81 { 82 int T,cas = 1; 83 scanf("%d",&T); 84 while(T--) 85 { 86 scanf("%s",a); 87 int len = 0; 88 for(int i =0;a[i] != '