HDU 4886 tiankeng's restaurant (II) (hash)

Source: Internet
Author: User

Question:

Find the first smallest lexicographic string that does not appear in the text string. ,


Train of Thought Analysis:

At the beginning, use the suffix array to write data, and then sort the data according to the SA order. You can know which string does not appear.

But the question gets doubled...

If you think about it yourself, we use the SA array, that is, to know whether this string has appeared, that is, to judge whether it is repeated, but it wastes O (N * lg n )...

Judge why the repeat did not expect hash.

Hash each length substring and use the octal representation to obtain an incremental hash value.

Store it into a hash array. Find the subscript of the first empty hash, that is, the first hash that does not appear, and then break down each bit to get the answer. This code HDU rank 1... (2014-07-30 ...)


#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;bool hash[1000007];char str[1000007];int pow[10];void print(int x,int dep,int cnt){    if(x)    {        print(x/8,dep+1,cnt);        putchar(x%8+'A');        return;    }    else if(dep<cnt)    {        while(dep<cnt)        {            putchar('A');            dep++;        }    }}int main(){    pow[0]=1;    for(int i=1; i<=8; i++)pow[i]=pow[i-1]*8;    int T;    scanf("%d",&T);getchar();    while(T--)    {        gets(str);        int n=strlen(str);        bool found=false;        for(int len=1; len<=7; len++)        {            memset(hash,false,sizeof hash);            int init=0;            int m=min(len,n);            for(int i=0; i<m; i++)            {                init=str[i]-'A'+init*8;            }            hash[init]=true;            for(int i=len; i<n; i++)            {                init-=pow[len-1]*(str[i-len]-'A');                init=init*8+str[i]-'A';                hash[init]=true;            }            for(int i=0; i<pow[len]; i++)            {                if(!hash[i])                {                    print(i,0,len);                    puts("");                    found=true;                    break;                }            }            if(found)break;        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.