HDU - 5558 Alice's Classified Message

Time Limit: 8000MS Memory Limit: 131072KB 64bit IO Format: %I64d & %I64u

 Status

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×10^6 .

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

后缀数组题,,用D3构造sa数组AC用了将近1s的时间,,看了前几名跑得快的,是用了后缀自动机,这个以后再学吧。。
这题就是按照题目的意思模拟就好,也没什么难度
#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define MX 111111
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<int, int>, LL> PIII;
typedef pair<int, int> PII;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
char s[MX];
int arr[MX*3],sa[MX*3],n;
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[MX],wb[MX],wv[MX],c[MX];
int c0(int *r,int a,int b) {
    return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];
}
int c12(int k,int *r,int a,int b) {
    if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);
    else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];
}
void _sort(int *r,int *a,int *b,int n,int m) {
    int i;
    for(i=0; i<n; i++) wv[i]=r[a[i]];
    for(i=0; i<m; i++) c[i]=0;
    for(i=0; i<n; i++) c[wv[i]]++;
    for(i=1; i<m; i++) c[i]+=c[i-1];
    for(i=n-1; i>=0; i--) b[--c[wv[i]]]=a[i];
    return;
}
void dc3(int *r,int *sa,int n,int m) {
    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;
    r[n]=r[n+1]=0;
    for(i=0; i<n; i++) if(i%3!=0) wa[tbc++]=i;
    _sort(r+2,wa,wb,tbc,m);
    _sort(r+1,wb,wa,tbc,m);
    _sort(r,wa,wb,tbc,m);
    for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++)
        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;
    if(p<tbc) dc3(rn,san,tbc,p);
    else for(i=0; i<tbc; i++) san[rn[i]]=i;
    for(i=0; i<tbc; i++) if(san[i]<tb) wb[ta++]=san[i]*3;
    if(n%3==1) wb[ta++]=n-1;
    _sort(r,wb,wa,ta,m);
    for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i;
    for(i=0,j=0,p=0; i<ta && j<tbc; p++)
        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];
    for(; i<ta; p++) sa[p]=wa[i++];
    for(; j<tbc; p++) sa[p]=wb[j++];
    return;
}
int Rank[MX],height[MX];
void calheight(int *r,int n) { //calheight(arr,n); height下标取1~n
    int i,j,k=0;
    for(i=1; i<=n; i++) Rank[sa[i]]=i;
    for(i=0; i<n; height[Rank[i++]]=k)for(k?k--:k,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);
}
PII check(int x) {
    int X=Rank[x];
    int minn=INF;
    int len=0;
    int nowmin=INF;
    for(int i=X; i>=1; i--) {
        nowmin=min(nowmin,height[i]);
        if(nowmin<len) break;
        if(sa[i-1]<x) {
            if(len<nowmin) {
                len=nowmin;
                minn=sa[i-1];
            } else if(len==nowmin) minn=min(sa[i-1],minn);
        }
    }
    nowmin=INF;
    for(int i=X+1; i<=n; i++) {
        nowmin=min(nowmin,height[i]);
        if(nowmin<len) break;
        if(sa[i]<x) {
            if(len<nowmin) {
                len=nowmin;
                minn=sa[i];

            } else if(len==nowmin) minn=min(sa[i],minn);
        }
    }
    return PII(len,minn);
}
int main() {
    FIN;
    int _;
    cin>>_;
    int _case=0;
    while(_--) {
        printf("Case #%d:\n",++_case);
        scanf("%s",s);
        n=strlen(s);
        for(int i=0; i<n; i++) arr[i]=s[i];
        arr[n]=0;
        dc3(arr,sa,n+1,128);
        calheight(arr,n);
        printf("-1 %d\n",arr[0]);
        int i=1;
        while(i<n) {
            PII k=check(i);
            if(k.first==0) {
                printf("-1 %d\n",arr[i]);
                i++;
            } else {
                printf("%d %d\n",k.first,k.second);
                i+=k.first;;
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值