-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest Subsequence.cpp
More file actions
90 lines (71 loc) · 1.46 KB
/
Copy pathLongest Subsequence.cpp
File metadata and controls
90 lines (71 loc) · 1.46 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
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 26;
typedef struct node{
int pos;
bool exist;
struct node * next[MAXN];
} TrieNode, * pTrieNode;
TrieNode Memory[1000000]; //先分配内存, 动态分配太耗时
int allocp = 0;
int MAXL = 0;
pTrieNode CreateTrie()
{
pTrieNode temp = &Memory[allocp ++];
temp->pos = 0;
temp->exist = false;
memset(temp->next, 0, sizeof(temp->next));
return temp;
}
void InsertTrie(pTrieNode & root, string str)
{
pTrieNode temp = root;
int i = 0, k;
while(str[i])
{
k = str[i] - 'a';
if(temp->next[k] == NULL)
temp->next[k] = CreateTrie();
temp = temp->next[k];
i++;
}
temp->exist = true;
}
void search(pTrieNode & root, string m, int st, int cur)
{
for(int i = 0; i < MAXN; i++)
{
pTrieNode & p = root->next[i];
if(p != NULL)
{
p->pos = m.find_first_of('a' + i, st);
// cout << "Find " << (char)('a' + i) << " at " << p->pos << " ,cur " << cur << endl;
if(p->pos != -1)
{
cur ++;
if(p->exist)
MAXL = cur > MAXL ? cur : MAXL;
search(p, m, p->pos + 1, cur);
cur --;
}
}
}
}
int main()
{
int n;
string s, match;
pTrieNode root = CreateTrie();
scanf("%d", &n);
getchar();
while(n --)
{
getline(cin, s);
InsertTrie(root, s);
}
getline(cin, match);
search(root, match, 0, 0);
printf("%d\n", MAXL);
return 0;
}