zoj 3228 Searching the String 모 문자열 은 모두 몇 개의 하위 문자열 을 포함 합 니까? (중첩 과 겹 치지 않 음)

6030 단어 acm_데이터 구조
Little jay really hates to deal with string. But moondy likes it very much, and she's so mischievous that she often gives jay some dull problems related to string. And one day, moondy gave jay another problem, poor jay finally broke out and cried, " Who can help me? I'll bg him! "
So what is the problem this time?
First, moondy gave jay a very long string A. Then she gave him a sequence of very short substrings, and asked him to find how many times each substring appeared in string A. What's more, she would denote whether or not founded appearances of this substring are allowed to overlap.
At first, jay just read string A from begin to end to search all appearances of each given substring. But he soon felt exhausted and couldn't go on any more, so he gave up and broke out this time.
I know you're a good guy and will help with jay even without bg, won't you?
Input
Input consists of multiple cases( <= 20 ) and terminates with end of file.
For each case, the first line contains string A ( length <= 10^5 ). The second line contains an integer N ( N <= 10^5 ), which denotes the number of queries. The next N lines, each with an integertype and a string a ( length <= 6 ), type = 0 denotes substring a is allowed to overlap and type = 1 denotes not. Note that all input characters are lowercase.
There is a blank line between two consecutive cases.
Output
For each case, output the case number first ( based on 1 , see Samples ).
Then for each query, output an integer in a single line denoting the maximum times you can find the substring under certain rules.
Output an empty line after each case.
Sample Input
ab
2
0 ab
1 ab

abababac
2
0 aba
1 aba

abcdefghijklmnopqrstuvwxyz
3
0 abc
1 def
1 jmn

Sample Output
Case 1
1
1

Case 2
3
2

Case 3
1
1
0


Hint
In Case 2,you can find the first substring starting in position (indexed from 0) 0,2,4, since they're allowed to overlap. The second substring starts in position 0 and 4, since they're not allowed to overlap.
For C++ users, kindly use scanf to avoid TLE for huge inputs.
#include#include#includeusing namespace std;const int maxn=110000;struct Node{    int index;    Node* next[26];    Node* fail;};Node temp[maxn*6];int tp;Node *root;//0 중첩 가능  1. int ans [2] [maxn] 중첩 불가;int pos[maxn];//1 중첩 불가 시 pos [i] = - 1... / / 이렇게 하면 p - pos [i] > = len [i]. (p 는 현재 성공 적 으로 일치 하 는 위치) / / pos [i] 는 모드 i 가 지난번 에 성공 적 으로 일치 하 는 위치 char str [maxn] [8];int flag[maxn];void reset(Node* p){    p->index=-1;    for(int i=0;i<26;i++) p->next[i]=NULL;    p->fail=root;//important    if (p = = 루트) p - > fail = NULL; / important} / / void init () {초기 화    memset(ans,0,sizeof(ans));    memset(pos,-1,sizeof(pos));    tp=0;    root=&temp[tp++];    reset (root);} / / 사전 트 리 삽입 int insert (char * word, int index) {    Node *p=root;    for(int i=0;word[i];i++)    {        int x=word[i]-'a';        if(p->next[x]==NULL)        {            p->next[x]=&temp[tp++];            reset(p->next[x]);        }        p=p->next[x];    }    if(p->index==-1) p->index=index;    return p - > index;} / / 구조 실패 포인터 Node * que [maxn * 6]; int front, rear; void DFA () {    Node* p=root;    front=rear=0;    que[rear++]=p;    while(front    {        Node* t=que[front++];        for(int i=0;i<26;i++)        {            Node* cnt=t->next[i];            if(cnt!=NULL)            {                Node* fath=t->fail;                while(fath!=NULL&&fath->next[i]==NULL)                {                    fath=fath->fail;                }                if(fath!=NULL)                {                    cnt->fail=fath->next[i];                }                else                {                    cnt->fail=p;//important                }                que[rear++]=cnt;            }        }    }}char mat [max n]; / / 문자열 void query () {    Node* p=root;    Node* cnt;    Node* fath;    for(int i=0;mat[i];i++)    {        int x=mat[i]-'a';        cnt=p->next[x];        if(cnt!=NULL)        {            for(fath=cnt;fath!=NULL;fath=fath->fail)            {                if(fath->index!=-1)                {                    ans[0][fath->index]++;                    int len=strlen(str[fath->index]);                    if(i-pos[fath->index]>=len)                    {                        ans[1][fath->index]++;                        pos[fath->index]=i;                    }                }            }            p=cnt;        }        else        {            fath=p->fail;            while(fath!=NULL&&fath->next[x]==NULL) fath=fath->fail;            if(fath!=NULL)            {                cnt=fath->next[x];                for(fath=cnt;fath!=NULL;fath=fath->fail)                {                    if(fath->index!=-1)                    {                        ans[0][fath->index]++;                        int len=strlen(str[fath->index]);                        if(i-pos[fath->index]>=len)                        {                            ans[1][fath->index]++;                            pos[fath->index]=i;                        }                    }                }                p=cnt;            }            else            {                p=root;            }        }    }}int main(){    int cas=1;    while(scanf("%s",mat)==1)    {        init();        int n;scanf("%d",&n);        for(int i=0;i        {            scanf("%d%s",&flag[i],str[i]);            insert(str[i],i);        }        DFA();        query();//0:overlap   1:not overlap        printf("Case %d",cas++);        for(int i=0;i        {            int index=insert(str[i],-1);            printf("%d",ans[flag[i]][index]);        }printf("");    }    return 0;}

좋은 웹페이지 즐겨찾기