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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "message.h"
// Grzegorz Guspiel
#include <bits/stdc++.h>
#define debug if(0) if(MyNodeId() == 0)
using namespace std;
 
#define REP(i,n) for(int i=0;i<int(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second

template<typename T> void maxE(T& a, const T& b) { a = max(a, b); }
template<typename T> void minE(T& a, const T& b) { a = min(a, b); }
template<typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& rhs) {
    out << "(" << rhs.st << "," << rhs.nd << ")";
    return out;
}
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
    out << "[";
    FOREACH (i, v) out << *i << ", ";
    out << "]";
    return out;
}

typedef pair<int, int> PII;

const int MAXN = 100100;
const int INF = 1000 * 1000 * 1000 + 1000;

PII operator+(const PII& a, const PII& b) { return mp(a.st + b.st, a.nd + b.nd); }

int n, m;
char buf1[MAXN * 2];
char buf2[MAXN * 2];
int s;
int nodes;

bool printed;
PII compute(int i, int j, PII lv, PII mv, PII tv, bool noassert = false) {
    if (i > n || j > m) return mp(-1, -1);
    PII r = min(tv + mp(1, 0), lv + mp(1, 0));        
    char c1 = buf1[i - 1];
    char c2 = buf2[j - 1];
    if (c1 == c2) {
        minE(r, mv + mp(0, 0));
    } else {
        minE(r, mv + mp(1, c2 - c1 > 0));
    }
    if (i == n && j == m) {
        if (!printed)
            printf("%d %d\n", r.st, r.nd);
        printed = 1;
    }
    return r;
}

PII slv[2][MAXN]; int slvn[2];
PII stv[2][MAXN]; int stvn[2];

void pbSlv(int i, PII a) { slv[i][slvn[i]++] = a; }
void pbStv(int i, PII a) { stv[i][stvn[i]++] = a; }
PII backSlv(int i) { return slv[i][slvn[i] - 1]; }
PII backStv(int i) { return stv[i][stvn[i] - 1]; }

void doSquare(int si, int sj, vector<PII> lv, PII mv, vector<PII> tv,
        vector<PII>& obv, vector<PII>& orv) {
    lv.insert(lv.begin(), mv);
    tv.insert(tv.begin(), mv);
    slvn[0] = stvn[0] = 0;
    FOREACH (i, lv) pbSlv(0, *i);
    FOREACH (i, tv) pbStv(0, *i);

    REP (k, s) {
        int iv = k % 2;
        int inv = (k + 1) % 2;
        slvn[inv] = stvn[inv] = 0;
        for (int i = k; i < s; i++)  {
            pbSlv(inv, compute(si + i, sj + k, 
                        slv[iv][i - k + 1], slv[iv][i - k], (i==k) ? stv[iv][1] : backSlv(inv)));
        }
        obv.pb(backSlv(inv));
        for (int j = k; j < s; j++) {
            pbStv(inv, compute(si + k, sj + j, 
                        (j==k) ? slv[iv][1] : backStv(inv),
                        stv[iv][j - k],
                        stv[iv][j - k + 1]));
        }
        orv.pb(backStv(inv));
    }
}

void sendLv(int to, const vector<PII>& lv) {
    PutInt(to, 0);
    assert(SIZE(lv) == s);
    REP (i, s) { PutInt(to, lv[i].st); PutInt(to, lv[i].nd); }
    Send(to);
}
void sendMv(int to, PII mv) {
    PutInt(to, 1);
    PutInt(to, mv.st);
    PutInt(to, mv.nd);
    Send(to);
}
void sendTv(int to, const vector<PII>& tv) {
    PutInt(to, 2);
    assert(SIZE(tv) == s);
    REP (i, s) { PutInt(to, tv[i].st); PutInt(to, tv[i].nd); }
    Send(to);
}

int owner_(int i, int j) {
    i--; j--;
    i /= s; j /= s;
    return j - i;
}
int owner(int i, int j) {
    return owner_(i, j) - owner_(n, 0);
}

PII GetPair(int who) {
    int a = GetInt(who);
    int b = GetInt(who);
    return mp(a, b);
}

void work() {
    int si = 1;
    int sj = 1;
    while (owner(si, sj) != MyNodeId()) {
        if (owner(si, sj) < MyNodeId()) sj += s;
        else si += s;
    }
    debug cout << "work from si " << si << " sj " << sj << endl;
    while (si <= n && sj <= m) {
        vector<PII> lv, tv; PII mv;
        REP (cnt, 3) {
            int who = Receive(-1);
            int type = GetInt(who);
            if (type == 0) {
                REP (i, s) lv.pb(GetPair(who));
            } else if (type == 1) {
                mv = GetPair(who);
            } else if (type == 2) {
                REP (i, s) tv.pb(GetPair(who));
            }
        }
        vector<PII> obv, orv;
        doSquare(si, sj, lv, mv, tv, obv, orv);
        if (sj + s <= m) sendLv(owner(si, sj + s), orv);
        if (si + s <= n) sendTv(owner(si + s, sj), obv);
        assert(obv.back() == orv.back());
        if (sj + s <= m && si + s <= n) sendMv(owner(si + s, sj + s), obv.back());
        //debug cout << "obv " << obv << endl;
        si += s; sj += s;
    }
}

int main() {
    srand(15);
    scanf("%d%d", &n, &m);
    scanf("%s%s", buf1, buf2);

    nodes = NumberOfNodes();
    s = 1;
    while (nodes <= owner(1, m)) s++;
    debug cout << "nodes " << nodes << " s " << s << endl;

    if (MyNodeId() == 0) {
        for (int i = 1; i <= n; i += s) {
            vector<PII> lv;
            REP (j, s) lv.pb(mp(i + j, 0));
            sendLv(owner(i, 1), lv);
            sendMv(owner(i, 1), mp(i - 1, 0));
        }
        for (int j = 1; j <= m; j += s) {
            vector<PII> tv;
            REP (i, s) tv.pb(mp(i + j, 0));
            sendTv(owner(1, j), tv);
            if (j > 1)
                sendMv(owner(1, j), mp(j - 1, 0));
        }
    }

    work();
    return 0;
}