blob: 2517d816a3e093b8f98ef7695d33dd784e0887bf (
plain)
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
|
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int const Letters = 4;
int const NA = -1;
struct Node
{
int next [Letters];
Node ()
{
fill (next, next + Letters, NA);
}
bool isLeaf () const
{
return (next[0] == NA && next[1] == NA && next[2] == NA && next[3] == NA);
}
void print()
{
if (isLeaf())
cout << "---- " << endl;
else
cout << "A:" << next[0] << " C:" << next[1]
<< " G:" << next[2] << " T:" << next[3] << endl;
}
};
struct Trie
{
vector<Node> nodes;
Trie(const vector <string>& patterns)
{
int v = 1;
nodes.push_back(Node());
for (int i = 0; i < patterns.size(); i++) {
int n = 0;
const string& pattern = patterns[i];
for (int j = 0; j < pattern.size(); j++) {
int c = letterToIndex(pattern[j]);
int next = nodes[n].next[c];
if (next != NA) {
n = next;
} else {
nodes[n].next[c] = v;
n = v;
nodes.push_back(Node());
v += 1;
}
}
}
}
int letterToIndex (char letter)
{
switch (letter)
{
case 'A': return 0; break;
case 'C': return 1; break;
case 'G': return 2; break;
case 'T': return 3; break;
default: assert (false); return -1;
}
}
void print()
{
for (int i = 0; i < nodes.size(); i++) {
cout << i << " " ;
nodes[i].print();
}
}
bool prefixMatch(const string& text)
{
int n = 0;
for( int i = 0; i < text.size(); i++) {
int next = nodes[n].next[letterToIndex(text[i])];
if (next == NA)
break;
n = next;
}
return nodes[n].isLeaf();
}
void match(const string& text, vector<int>& result)
{
for (int i = 0; i < text.size(); i++) {
if (prefixMatch(&text[i]))
result.push_back(i);
}
}
};
vector <int> solve (const string& text, int n, const vector <string>& patterns)
{
vector <int> result;
Trie t(patterns);
/* t.print(); */
t.match(text, result);
return result;
}
int main (void)
{
string t;
cin >> t;
int n;
cin >> n;
vector <string> patterns (n);
for (int i = 0; i < n; i++)
{
cin >> patterns[i];
}
vector <int> ans;
ans = solve (t, n, patterns);
for (int i = 0; i < (int) ans.size (); i++)
{
cout << ans[i];
if (i + 1 < (int) ans.size ())
{
cout << " ";
}
else
{
cout << endl;
}
}
return 0;
}
|