Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- string a[25];
- int n;
- int l_dist(const string& s1, const string& s2) {
- int M = s1.size() + 1;
- int N = s2.size() + 1;
- int D[M][N];
- D[0][0] = 0;
- for (int j = 1; j < N; j++) {
- D[0][j] = D[0][j- 1] + 1;
- }
- for (int i = 1; i < M; i++) {
- D[i][0] = D[(i - 1)][0] + 1;
- for (int j = 1; j < N; j++) {
- D[i][j] = min(min(D[i][j - 1] + 1, D[(i - 1)][j] + 1),
- D[(i - 1)][j - 1] + ((s1[i - 1] == s2[j - 1]) ? 0 : 2));
- }
- }
- return D[(M - 1)][N - 1];
- };
- // int l_dist(const string& s1, const string& s2) {
- // int M = s1.size() + 1;
- // int N = s2.size() + 1;
- // int D[M][N];
- // D[0][0] = 0;
- // for (int j = 1; j < N; j++) {
- // D[0][j] = D[0][j- 1] + 1;
- // }
- // for (int i = 1; i < M; i++) {
- // D[i][0] = D[i - 1][0] + 1;
- // for (int j = 1; j < N; j++) {
- // std::vector<int> tmp = {
- // D[i - 1][j] + 1,
- // D[i][j - 1] + 1,
- // D[i - 1][j - 1] + ((s1[i - 1] == s2[j - 1]) ? 0 : 2)
- // };
- // D[i][j] = *min_element(tmp.begin(), tmp.end());
- // }
- // }
- // return D[M - 1][N - 1];
- // };
- template<typename T>
- typename T::size_type GeneralizedLevenshteinDistance(const T &source,
- const T &target,
- typename T::size_type insert_cost = 1,
- typename T::size_type delete_cost = 1,
- typename T::size_type replace_cost = 2) {
- if (source.size() > target.size()) {
- return GeneralizedLevenshteinDistance(target, source, delete_cost, insert_cost, replace_cost);
- }
- using TSizeType = typename T::size_type;
- const TSizeType min_size = source.size(), max_size = target.size();
- std::vector<TSizeType> lev_dist(min_size + 1);
- lev_dist[0] = 0;
- for (TSizeType i = 1; i <= min_size; ++i) {
- lev_dist[i] = lev_dist[i - 1] + delete_cost;
- }
- for (TSizeType j = 1; j <= max_size; ++j) {
- TSizeType previous_diagonal = lev_dist[0], previous_diagonal_save;
- lev_dist[0] += insert_cost;
- for (TSizeType i = 1; i <= min_size; ++i) {
- previous_diagonal_save = lev_dist[i];
- if (source[i - 1] == target[j - 1]) {
- lev_dist[i] = previous_diagonal;
- } else {
- lev_dist[i] = std::min(std::min(lev_dist[i - 1] + delete_cost, lev_dist[i] + insert_cost), previous_diagonal + replace_cost);
- }
- previous_diagonal = previous_diagonal_save;
- }
- }
- return lev_dist[min_size];
- }
- void gen_random(std::string& s, const int len) {
- s.resize(len);
- static const char alphanum[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- for (int i = 0; i < len; ++i) {
- s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
- }
- s[len] = 0;
- }
- int dist(string s1, string s2) {
- int m = s1.size();
- int n = s2.size();
- vector<int> v0(n + 1);
- vector<int> v1(n + 1);
- for (int i = 0; i < n + 1; i++)
- v0[i] = i;
- for (int i = 0; i < m; i++) {
- v1[0] = i + 1;
- for (int j = 0; j < n; j++) {
- int deletionCost = v0[j + 1] + 1;
- int insertionCost = v1[j] + 1;
- int substitutionCost = v0[j];
- if (s1[i] != s2[j])
- substitutionCost += 2;
- v1[j + 1] = min(min(deletionCost, insertionCost), substitutionCost);
- }
- swap(v0, v1);
- }
- return v0[n];
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(NULL);
- srand(time(NULL));
- freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- // ifstream in("input.txt");
- while (n < 25 and getline(cin, a[n])) n++;
- n--;
- int ans = -1, s1 = 0, s2 = 0;
- // for (int i = 0; i < n; i++) {
- // for (int j = 0; j < n; j++) {
- // if (i != j) {
- // int ld = l_dist(a[i], a[j]);
- // if (ans == -1 or ld < ans) {
- // ans = ld;
- // s1 = i + 1;
- // s2 = j + 1;
- // }
- // // ld =Z GeneralizedLevenshteinDistance<string>(a[i], a[j]);
- // cout << i + 1 << " " << j + 1 << " " << ld << endl;
- // }
- // }
- // }
- // cout << s1 << " " << s2 << " " << ans << endl;
- string a, b;
- for (int i = 1; i < 50; i ++) {
- for (int j = 1; j < 50; j++) {
- for (int k = 0; k < 10; k++) {
- gen_random(a, i);
- gen_random(b, j);
- int ans1 = l_dist(b, a);
- int ans2 = dist(b, a);
- if (ans1 != ans2) {
- std::cout << a << std::endl;
- std::cout << b << std::endl;
- }
- }
- }
- }
- // cout << l_dist("a", "aa");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement