-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunningletters.cpp
75 lines (62 loc) · 1.35 KB
/
runningletters.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
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void print_vec(vector<T> v) {
for (int i=0; i<v.size(); i++) {
cout << v[i] << ", ";
}
cout << endl;
}
vector<string> tokenize(string s, char token) {
vector<string> t;
size_t current;
size_t next = -1;
do {
current = next + 1;
next = s.find_first_of(token, current);
t.push_back(s.substr(current, next-current));
} while (next != string::npos);
return t;
}
string operator * (string a, unsigned int b) {
string output = "";
while (b--) {
output += a;
}
return output;
}
int compute_prefix_kmp(string a) {
int len = a.length();
int pi[len];
pi[0] = 0;
for (int i=1; i<len; i++) {
int j = pi[i-1];
while (j > 0 && a[i]!=a[j]) {
j = pi[j-1];
}
if (a[i] == a[j]) {
j++;
}
pi[i] = j;
}
// pi[len-1] stores the period of the string
return len - pi[len - 1];
}
int main() {
string running;
string skip;
getline(getline(cin, skip, '"'), running, '"');
vector<string> tok = tokenize(running, ' ');
string sign = "";
// Create the string resulting from the input
for (int i=0; i < tok.size(); i+=2) {
string text = tok[i+1] * atoi(tok[i].c_str());
sign += text;
}
//cout << "sign: " << sign << endl;
// Compute the prefix for the entire sign string
int sol = compute_prefix_kmp(sign);
cout << sol << endl;
return 0;
}