вторник, 17 марта 2020 г.

воскресенье, 15 марта 2020 г.

сортировка по японски

https://pastebin.com/tzVHBsub
c++

#include <bits/stdc++.h>

#define long long long int
using namespace std;

// @author: pashka

int main() {
    ios::sync_with_stdio(false);

    vector<string> a;
    string s;
    while (cin >> s) {
//        if (s == ".")
//            break;
        a.push_back(s);
    }

    sort(a.begin(), a.end(), [](string &a, string &b) -> bool {
        int i = 0;
        int j = 0;
        bool first = true;
        int za = 0;
        int zb = 0;
        while (i < a.size() && j < b.size()) {
            if (isdigit(a[i])) {
                if (isdigit(b[j])) {
                    while (a[i] == '0') {
                        if (first) za++;
                        i++;
                    }
                    int li = i;
                    while (isdigit(a[i])) {
                        i++;
                    }
                    int ri = i;
                    while (b[j] == '0') {
                        if (first) zb++;
                        j++;
                    }
                    int lj = j;
                    while (isdigit(b[j])) {
                        j++;
                    }
                    int rj = j;
                    if (ri - li != rj - lj) {
                        return ri - li < rj - lj;
                    }
                    for (int t = 0; t < ri - li; t++) {
                        if (a[li + t] != b[lj + t]) {
                            return a[li + t] < b[lj + t];
                        }
                    }
                    if (za != zb) first = false;
                } else {
                    return true;
                }
            } else {
                if (isdigit(b[j])) {
                    return false;
                } else {
                    if (a[i] < b[j]) return true;
                    if (a[i] > b[j]) return false;
                    i++;
                    j++;
                }
            }
        }
        if (i == a.size() && j == b.size()) {
            if (first)
                return a < b;
            else
                return za > zb;
        }
        return i == a.size() && j < b.size();
    });

    for (string x : a) {
        cout << x << "\n";
    }

    return 0;
}
//293566DZ