HDU 4644 Bwt (KMP)

Source: Internet
Author: User

After reading the question, you can easily think of how to restore the given array to the original array.

After the original array is restored, both the AC automatic machine and KMP can be solved-although I think KMP will time out.


So how to restore this string is a difficult question...


GC $ aaac

1234567

Changed

$ Aaaccg

3456271


Then you follow the sorted subscript.

Will find

$-> A-> C-> G

3 5 2 4 6 7

The original string is restored.


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define maxn 100186using namespace std;struct node{    char ch;    int index;    bool operator < (const node & cmp)const    {        return ch<cmp.ch;    }}save[maxn];char t[maxn];char str[maxn],txt[maxn];int next[maxn];void getnext(int len){    next[0]=0;next[1]=0;    for(int i=1;i<len;i++)    {        int j=next[i];        while(j && txt[j]!=txt[i])j=next[j];        next[i+1]=txt[j]==txt[i]?j+1:0;    }}void find(int n,int m){    int j=0;    for(int i=0;i<n;i++)    {        while(j&&txt[j]!=str[i])j=next[j];        if(txt[j]==str[i])j++;        if(j==m){printf("YES\n");return ;}    }    printf("NO\n");}int main(){    while(scanf("%s",t)!=EOF)    {        int len=strlen(t);        for(int i=0;i<len;i++)        {            save[i].ch=t[i];            save[i].index=i;        }        stable_sort(save,save+len);        int now=save[0].index;        for(int i=0;i<len-1;i++)        {            str[i]=save[now].ch;            now=save[now].index;        }        int q;        scanf("%d",&q);        while(q--)        {            scanf("%s",txt);            int m=strlen(txt);            getnext(m);            find(len-1,m);        }    }    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.