hdu5558 Alice's Classified Message

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012632877/article/details/49839907

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[ab] means a substring of S ranging from S[a] to S[b] (0ab<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+K1] (0T<iT+KN) and P=S[ii+K1](i+KN). In other words, P is a substring of S, of which starting address is within [0...i1], and P is also a prefix of S[i...N1]. 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 (T50). 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
 

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<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
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<m;i++)c[i]=0;
        for(int i=0;i<n;i++)c[x[i]=s[i]]++;
        for(int i=1;i<m;i++)c[i]+=c[i-1];
        for(int i=n-1;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<n;i++)sa2[p++]=i;
            for(int i=0;i<n;i++)if(sa[i]>=k)sa2[p++]=sa[i]-k;
            for(int i=0;i<m;i++)c[i]=0;
            for(int i=0;i<n;i++)c[x[sa2[i]]]++;
            for(int i=1;i<m;i++)c[i]+=c[i-1];
            for(int i=n-1;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;i++)
                x[sa[i]]=sa2[sa[i-1]]==sa2[sa[i]]&&sa2[sa[i-1]+k]==sa2[sa[i]+k]?p-1:p++;
            if(p>=n)break;
            m=p;
        }
    }
    int rank[maxn];
    void calheignt(){
        int k=0;
        for(int i=0;i<n;i++)rank[sa[i]]=i;h[0]=0;
        for(int i=0;i<n-1;i++){
            if(k)k--;
            int j=sa[rank[i]-1];
            while(s[i+k]==s[j+k])k++;
            h[rank[i]]=k;
        }
    }
    int _st[maxn][20],pow2[20],n_st[maxn][20];
    void st_build(){
        pow2[0]=1;for(int i=1;i<=19;i++)pow2[i]=pow2[i-1]*2;
        for(int i=n-1;i>=0;i--){
            _st[i][0]=h[i];n_st[i][0]=sa[i];
            for(int j=1,k=1;2*j+i-1<n;j<<=1,k++){
                _st[i][k]=min(_st[i][k-1],_st[i+j][k-1]);
                n_st[i][k]=min(n_st[i][k-1],n_st[i+j][k-1]);
            }
        }
    }
    int st_query(int l,int r){
        int k=(int)log2(r-l+1);
        return min(_st[l][k],_st[r-pow2[k]+1][k]);;
    }
    int n_st_query(int l,int r){
        int k=(int)log2(r-l+1);
        return min(n_st[l][k],n_st[r-pow2[k]+1][k]);
    }
}S;
int queryl(int x){
    int l=1,r=x-1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(S.n_st_query(mid,x-1)<S.sa[x])l=mid+1;
        else r=mid-1;
    }
    return l-1;
}
int queryr(int x){
    int l=x+1,r=n-1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(S.n_st_query(x+1,mid)<S.sa[x])r=mid-1;
        else l=mid+1;
    }
    return l;
}
int queryll(int x,int v){
    int l=1,r=x-1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(S.st_query(mid+1,x)<v)l=mid+1;
        else r=mid-1;
    }
    return r+1;
}
int queryrr(int x,int v){
    int l=x+1,r=n-1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(S.st_query(x+1,mid)<v)r=mid-1;
        else l=mid+1;
    }
    return l-1;
}
void work(int no){
    printf("Case #%d:\n",no);
    scanf("%s",S.s);n=strlen(S.s);S.s[n]='@';n++;
    printf("-1 %d\n",S.s[0]);
    S.build();
    S.calheignt();
    S.st_build();
    for(int i=1;i<n-1;){
        i=S.rank[i];
        int l=queryl(i);
        int x=0;
        if(l>0)x=S.st_query(l+1,i);
        int r=queryr(i);
        int y=0;
        if(r<n)y=S.st_query(i+1,r);
        if(y>x){
            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<x){
            int c=queryll(i,x);
            int z=S.n_st_query(c,i);
            printf("%d %d\n",x,z);
            i=S.sa[i];i+=x;
        }else if(y==x){
            if(x==0){
                    i=S.sa[i];
                printf("-1 %d\n",S.s[i]);
                i++;
            }else{
                int c=queryll(i,y);
                int z=S.n_st_query(c,i);
                c=queryrr(i,x);
                z=min(z,S.n_st_query(i,c));
                printf("%d %d\n",x,z);
                i=S.sa[i];i+=x;
            }
        }
    }

}
int main(){
    int t;scanf("%d",&t);
    for(int i=1;i<=t;i++)work(i);
    return 0;
}



展开阅读全文

Alice's present

04-17

As a doll master, Alice owns a wide range of dolls, and each of them has a number tip on it's back, the tip can be treated as a positive integer. (the number can be repeated). One day, Alice hears that her best friend Marisa's birthday is coming , so she decides to sent Marisa some dolls for present. Alice puts her dolls in a row and marks them from 1 to n. Each time Alice chooses an interval from i to j in the sequence ( include i and j ) , and then checks the number tips on dolls in the interval from right to left. If any number appears more than once , Alice will treat this interval as unsuitable. Otherwise, this interval will be treated as suitable.nnThis work is so boring and it will waste Alice a lot of time. So Alice asks you for help .nnInputnnThere are multiple test cases. For each test case:nnThe first line contains an integer n ( 3≤ n ≤ 500,000) ,indicate the number of dolls which Alice owns.nnThe second line contains n positive integers , decribe the number tips on dolls. All of them are less than 2^31-1. The third line contains an interger m ( 1 ≤ m ≤ 50,000 ),indicate how many intervals Alice will query. Then followed by m lines, each line contains two integer u, v ( 1≤ u< v≤ n ),indicate the left endpoint and right endpoint of the interval. Process to the end of input.nnOutputnnFor each test case:nnFor each query, If this interval is suitable , print one line "OK". Otherwise, print one line ,the integer which appears more than once first.nnPrint an blank line after each case.nnSample Inputnn5n1 2 3 1 2n3n1 4n1 5n3 5n6n1 2 3 3 2 1n4n1 4n2 5n3 6n4 6nSample Outputnn1n2nOKnn3n3n3nOK 问答

Alice's mooncake shop

07-25

Problem DescriptionnThe Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Chinese people, dating back over 3,000 years to moon worship in China's Shang Dynasty. The Zhongqiu Festival is held on the 15th day of the eighth month in the Chinese calendar, which is in September or early October in the Gregorian calendar. It is a date that parallels the autumnal equinox of the solar calendar, when the moon is at its fullest and roundest. nnnThe traditional food of this festival is the mooncake. Chinese family members and friends will gather to admire the bright mid-autumn harvest moon, and eat mooncakes under the moon together. In Chinese, “round”(圆) also means something like “faultless” or “reuion”, so the roundest moon, and the round mooncakes make the Zhongqiu Festival a day of family reunion.nnAlice has opened up a 24-hour mooncake shop. She always gets a lot of orders. Only when the time is K o’clock sharp( K = 0,1,2 …. 23) she can make mooncakes, and We assume that making cakes takes no time. Due to the fluctuation of the price of the ingredients, the cost of a mooncake varies from hour to hour. She can make mooncakes when the order comes,or she can make mooncakes earlier than needed and store them in a fridge. The cost to store a mooncake for an hour is S and the storage life of a mooncake is T hours. She now asks you for help to work out a plan to minimize the cost to fulfill the orders.n nnInputnThe input contains no more than 10 test cases. nFor each test case:nThe first line includes two integers N and M. N is the total number of orders. M is the number of hours the shop opens. nThe next N lines describe all the orders. Each line is in the following format:nnmonth date year H RnnIt means that on a certain date, a customer orders R mooncakes at H o’clock. “month” is in the format of abbreviation, so it could be "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov" or "Dec". H and R are all integers. nAll the orders are sorted by the time in increasing order. nThe next line contains T and S meaning that the storage life of a mooncake is T hours and the cost to store a mooncake for an hour is S.nFinally, M lines follow. Among those M lines, the ith line( i starts from 1) contains a integer indicating the cost to make a mooncake during the ith hour . The cost is no more than 10000. Jan 1st 2000 0 o'clock belongs to the 1st hour, Jan 1st 2000 1 o'clock belongs to the 2nd hour, …… and so on.nn(0 问答

没有更多推荐了,返回首页