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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>
#include "message.h"

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair
#define X           first
#define Y           second

#define ULL         unsigned long long
#define LL          long long
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>
#define VLL    VC<LL>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

#define N_MAX 100000

char s[N_MAX+2];
char t[N_MAX+2];

int n,m;

void readData(){
    scanf("%d%d",&n,&m);
    if(n <= m){
        scanf("%s",s);
        scanf("%s",t);
    }else{
        swap(n,m);
        scanf("%s",t);
        scanf("%s",s);
        REP(i,n) s[i] = 'a'+(25-(s[i]-'a'));
        REP(i,m) t[i] = 'a'+(25-(t[i]-'a'));
    }
}

#define SH 20
#define INC (1 << SH)
#define INCPLUS (INC + 1)

int N, ID;

void distInit(){
    N = NumberOfNodes();
    ID = MyNodeId();
}

int bufSize;

LL solve(){
    bufSize = max(m/500,10);
    int n1 = ((LL)ID*n)/N;
    int n2 = ((LL)(ID+1)*n)/N;
    int dn = n2-n1;
    VLL dp0(dn+1); dp0[0] = n1*((LL)INC); FOR(i,1,dn+1) dp0[i] = dp0[i-1] + INC;
    VLL dp1(dn+1);
    int inBuf = 0;
    int outBuf = 0;
    REP(i,m){
        if (ID == 0) 
            dp1[0] = dp0[0] + INC;
        else{
            if (inBuf == 0){
                Receive(ID-1);
                inBuf = bufSize;
            }
            dp1[0] = GetLL(ID-1);
            inBuf--;
        }
        for(int j=1, jm=n1; j <= dn; j++, jm++){
            LL change = dp0[j-1] + ((s[jm] == t[i])?0:((s[jm]>t[i])?INC:INCPLUS));
            dp1[j] = min(min(dp1[j-1],dp0[j])+INC,change);
        }
        if (ID != N-1){
            PutLL(ID+1,dp1[dn]);
            outBuf++;
            if (outBuf == bufSize || i == m-1){
                Send(ID+1);
                outBuf = 0;
            }
        }
        swap(dp0,dp1);
    }
    return dp0[dn];
}

LL solveBrute(){
    VLL dp0(n+1,0); dp0[0] = 0; FOR(i,1,n+1) dp0[i] = dp0[i-1] + INC;
    VLL dp1(n+1);
    REP(i,m){
        dp1[0] = dp0[0] + INC;
        FOR(j,1,n+1){
            LL change = dp0[j-1] + ((s[j-1] == t[i])?0:((s[j-1]>t[i])?INC:INCPLUS));
            dp1[j] = min(min(dp1[j-1],dp0[j])+INC,change);
        }
        swap(dp0,dp1);
    }
    return dp0[n];
}

int main(int argc, char *argv[]){
    readData();
    distInit();
    LL res;
    if (1000000/n >= m || N == 1) 
        res = solveBrute();
    else
        res = solve();
    if (ID == N-1)
        printf("%d %d\n",(int)(res>>SH),(int)(res&(INC-1)));
    return 0;
}