zoj 3228 Searching the String 모 문자열 은 모두 몇 개의 하위 문자열 을 포함 합 니까? (중첩 과 겹 치지 않 음)
6030 단어 acm_데이터 구조
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;}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
2761 Feed the dogs 임의의 구간 의 k 소수 선분 트 리 + 구분 트 리 구하 기Feed the dogs Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Win...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.