-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cpp
More file actions
104 lines (91 loc) · 2.16 KB
/
Copy pathstrings.cpp
File metadata and controls
104 lines (91 loc) · 2.16 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <vector>
#include <cmath>
#include <bitset>
#include <set>
#include <complex>
#include <queue>
#include <stack>
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef long double ld;
using namespace std;
// returns vector of tokens of this string
// "hello there" --> {"hello", "there"}
vector<string> tokenize(const string x){
// tokenize by spaces
vector<string> ans;
char *dup = strdup(x.c_str());
char *tk = strtok(dup, " ");
while(tk){
ans.push_back(tk);
tk = strtok(NULL, " ");
}
return ans;
}
vector<string> split(const string x, const char a){
string u = x;
for(int i=0; i<u.length(); i++) if(u[i] == a) u[i] = ' ';
stringstream ss(u);
vector<string> ans;
string temp;
while(ss >> temp) ans.push_back(temp);
return ans;
}
/*
Returns the lyndon factorization of the string a.
a = w_1w_2w_3...w_k
where each w_i is simple, and w_i >= w_i+1 (lex compare)
a string is simple if it is STRICTLY smaller than any of
its nontrivial suffixes (smaller than any cyclic shifts)
the lyndon factorization of a string exists and is unique
time complexity: O(n)
*/
vector<string> lyndon(string s) {
vector<string> w;
int n = s.size();
int i = 0;
while (i < n) {
int j = i + 1, k = i;
while (j < n && s[k] <= s[j]) {
if (s[k] < s[j]) k = i;
else k++;
j++;
}
while (i <= k) {
w.pb(s.substr(i, j - k));
i += j - k;
}
}
return w;
}
/*
Returns the edit distance between the two strings.
Insertions, deletions, and substitutions.
time complexity: O(a.length() * b.length())
*/
int levenshtein(string a, string b) {
int n = a.length();
int m = b.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (min(i, j) == 0) dp[i][j] = max(i, j);
else dp[i][j] = min(dp[i - 1][j] + 1, min(dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] != b[j - 1])));
}
}
return dp[n][m];
}
int main(){
vector<string> res = split("hello:there", ':');
for(string s: res)
cout << s <<endl;
cout << levenshtein("hi", "ho") << endl;
return 0;
}