Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) begin(x),end(x)
- using namespace std;
- using ll = long long;
- template<typename T> void read(T& v) { cin >> v; }
- template<typename T1, typename T2> void read(pair<T1, T2>& v) { cin >> v.first >> v.second; }
- template<typename T> void read(vector<T>& v) { for (auto& val : v) read(val);}
- template<typename T, typename... Args> void read(T& v, Args&... vs) { read(v), read(vs...); }
- template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
- template<typename T> using max_heap = priority_queue<T, vector<T>, less<T>>;
- const int MAX_LEN = 35;
- int dp[MAX_LEN][MAX_LEN];
- int Hemming(string& a, string& b) {
- int n = a.size();
- int ans = 0;
- for (int i = 0; i < n; i++) {
- if (a[i] != b[i]) {
- ans++;
- }
- }
- return ans;
- }
- int Levenshtein(string& a, string& d) {
- int n = a.size();
- int m = d.size();
- for (int i = 0; i <= n; i++) {
- dp[i][0] = i;
- }
- for (int j = 0; j <= m; j++) {
- dp[0][j] = j;
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] != d[j - 1])});
- }
- }
- return dp[n][m];
- }
- int DamerauLevenshtein(string& a, string& d) {
- int n = a.size();
- int m = d.size();
- for (int i = 0; i <= n; i++) {
- dp[i][0] = i;
- }
- for (int j = 0; j <= m; j++) {
- dp[0][j] = j;
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] != d[j - 1])});
- if (i > 1 && j > 1 && a[i - 1] == d[j - 2] && d[j - 1] == a[i - 2]) {
- dp[i][j] = min(dp[i][j], dp[i - 2][j - 2] + 1);
- }
- }
- }
- return dp[n][m];
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- setlocale (LC_ALL,"Rus");
- FILE* input = freopen("data_ansi.txt", "r", stdin);
- string str;
- map<int, vector<string>> dict;
- while (getline(cin, str)) {
- dict[str.size()].push_back(str);
- }
- for (auto& [len, v] : dict) {
- cout << len << " ";
- {
- auto start = clock();
- ll dlt = 0;
- string pre = v[0];
- int sz = v.size();
- for (int i = 0; i < sz; i++) {
- int value = Hemming(pre, v[i]);
- dlt += value;
- pre = v[i];
- }
- auto finish = clock();
- //cout << "Hemming: " << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " Total delta " << dlt << endl;
- cout << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " " << dlt << " ";
- }
- {
- auto start = clock();
- ll dlt = 0;
- string pre = v[0];
- int sz = v.size();
- for (int i = 0; i < sz; i++) {
- int value = Levenshtein(pre, v[i]);
- dlt += value;
- pre = v[i];
- }
- auto finish = clock();
- //cout << "Levenshtein: " << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " Total delta " << dlt << endl;
- cout << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " " << dlt << " ";
- }
- {
- auto start = clock();
- ll dlt = 0;
- string pre = v[0];
- int sz = v.size();
- for (int i = 0; i < sz; i++) {
- int value = DamerauLevenshtein(pre, v[i]);
- dlt += value;
- pre = v[i];
- }
- auto finish = clock();
- //cout << "DamerauLevenshtein: " << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " Total delta " << dlt << endl;
- cout << (finish - start) * 1000.0 / CLOCKS_PER_SEC << " " << dlt << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement