-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix.cpp
executable file
·111 lines (106 loc) · 2.55 KB
/
prefix.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
/*
ID: paulius10
PROG: prefix
LANG: C++
*/
#include <fstream>
#include <set>
using namespace std;
struct node {
char letter;
bool end;
node *child;
node *sibling;
};
/*int magic(string &str, node *root, int startingIndex) {
int max = 0;
if (startingIndex >= str.length()) return max;
node *pointer = root;
for (int i = startingIndex; i < str.length(); i++) {
if (pointer == NULL) return max;
while (pointer->letter != str[i]) {
pointer = pointer->sibling;
if (pointer == NULL) return max;
}
if (pointer->end) {
int future = i - startingIndex + 1 + magic(str, root, i + 1);
if (future > max) max = future;
}
pointer = pointer->child;
}
return max;
}*/
int main() {
ifstream fin("prefix.in");
set<string> primitives;
while (true) {
string input;
fin >> input;
if (input == ".") break;
primitives.insert(input);
}
string str;
while (!fin.eof()) {
string input;
fin >> input;
str += input;
}
fin.close();
node *root = NULL;
for (set<string>::iterator it = primitives.begin(); it != primitives.end();
it++) {
node *pointer = root;
node *parent = NULL;
for (int i = 0; i < it->length(); i++) {
node *previous = NULL;
while (true) {
if (pointer == NULL) {
pointer = new node;
pointer->letter = (*it)[i];
pointer->end = (i == it->length() - 1);
pointer->child = NULL;
pointer->sibling = NULL;
if (previous != NULL) previous->sibling = pointer;
else if (parent != NULL) parent->child = pointer;
else root = pointer;
break;
}
if (pointer->letter == (*it)[i]) break;
previous = pointer;
pointer = pointer->sibling;
}
parent = pointer;
pointer = pointer->child;
}
}
bool dp[str.length() + 1];
dp[0] = true;
for (int i = 1; i <= str.length(); i++) dp[i] = false;
for (int i = 1; i <= str.length(); i++) {
for (set<string>::iterator it = primitives.begin();
it != primitives.end(); it++) {
bool impossible = false;
int j = i - 1;
for (int k = (*it).length() - 1; k >= 0; k--) {
if (j < 0 || (*it)[k] != str[j]) {
impossible = true;
break;
}
j--;
}
if (!impossible && dp[i - (*it).length()]) {
dp[i] = true;
break;
}
}
}
ofstream fout("prefix.out");
for (int i = str.length(); i >= 0; i--) {
if (dp[i]) {
fout << i << endl;
break;
}
}
fout.close();
return 0;
}