-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10679_uva.cpp
More file actions
79 lines (75 loc) · 1.26 KB
/
10679_uva.cpp
File metadata and controls
79 lines (75 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
void print_array(int * array,int size){
for ( int i=0;i<size;i++){
printf("%d",array[i]);
}
printf("\n");
}
void suffix_array(char * pattern,int *array)
{
int i=0,j=0;
int len=strlen(pattern);
while(i<len){
if(i==j) {
}
else if(pattern[i]==pattern[j]){
j++;
}
else{
while(j>0){
j=array[j-1];
if(pattern[i]==pattern[j])break;
}
if (pattern[i]==pattern[j]) j++;
}
array[i]=j;
i++;
}
}
char checkKMP(char *str,char *pattern)
{
int pat_len=strlen(pattern);
char res='n';
if(pat_len==0) return 'y';
else if (pat_len==1) {
int index=-1;
for (int i=0;i< strlen(str);i++){
if(pattern[0]==str[i]) return 'y';
}
return 'n';
}
int * array=new int[strlen(pattern)];
suffix_array(pattern,array);
int j=0;
for (int i=0;i<strlen(str);i++){
if(str[i]==pattern[j])j++;
else if (j>0){
j=array[j-1];
}
if(j==pat_len){
res='y';
break;
}
}
return res;
}
int main(int argc, char *argv[] ){
int k, n;
scanf("%d",&k);
char S[100001];
char q[1001];
while(k--){
scanf("%s",&S);
scanf("%d",&n);
while(n--){
scanf("%s",&q);
char res=checkKMP(S,q);
printf("%c\n",res);
}
}
}