-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubstr.cpp
178 lines (167 loc) · 2.23 KB
/
substr.cpp
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
You are given
N
N strings. You have to answer
Q
Q queries, each containing a string
S
S.
Write a program to print the answer to each query using the following rules:
If
S
S contains no string from the
N
N given strings as its substring, then print 0.
If
S
S contains multiple strings from the
N
N given strings as its substrings, then print -1.
If
S
S contains exactly one string from the
N
N given strings as its substring, then print the index of that string (1-indexed).
Input format
First line: Two space separated integers
N
N and
Q
Q
Next
N
N lines: Single word (containing lowercase alphabets)
Next
Q
Q lines: Single word (containing lowercase alphabets and denoting each query)
Output format
For each query, print the answer in a separate line.
Constraints
1
≤
N
≤
10000
1≤N≤10000
1
≤
Q
≤
10000
1≤Q≤10000
1
≤
1≤
l
e
n
g
t
h
length
o
f
of
e
a
c
h
each
s
t
r
i
n
g
string
≤
10
≤10
1
≤
|
S
|
≤
10000
1≤|S|≤10000
1
≤
1≤
S
u
m
Sum
o
f
of
a
l
l
all
|
S
|
≤
100000
|S|≤100000
SAMPLE INPUT
2 3
spider
mantis
spiderman
spidermantis
spidy
SAMPLE OUTPUT
1
-1
0
// Sample code to perform I/O:
cin >> name; // Reading input from STDIN
cout << "Hi, " << name << ".\n"; // Writing output to STDOUT
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/
// Write your code here
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> s;
vector<string>::iterator i;
int n,q,count;
cin>>n>>q;
while(n--)
{
string v;
cin>>v;
s.push_back(v);
}
while(q--)
{
string check;
count=0;
cin>>check;
size_t found;
int flag=0,ans;
int index=0;
for(i=s.begin();i!=s.end();i++)
{ index++;
found = check.find(*i);
if (found!=string::npos)
{
if(flag==0)
{
ans=index;
flag=1;
}
count++;
}
}
if(count==0) cout<<"0";
else if(count==1) cout<<ans;
else cout<<"-1";
cout<<endl;
}
return 0;
}