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
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <cctype>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <numeric>
#include <cmath>
#include "message.h"
using namespace std;

typedef vector <int > VI;
typedef vector < VI > VVI;
typedef long long LL;
typedef vector < LL > VLL;
typedef vector < double > VD;
typedef vector < string > VS;
typedef pair<int,int> PII;
typedef vector <PII> VPII;
typedef istringstream ISS;

#define ALL(x) x.begin(),x.end()
#define REP(i,n) for (int i=0; i<(n); ++i)
#define FOR(var,pocz,koniec) for (int var=(pocz); var<=(koniec); ++var)
#define FORD(var,pocz,koniec) for (int var=(pocz); var>=(koniec); --var)
#define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); ++it)
#define PB push_back
#define PF push_front
#define MP(a,b) make_pair(a,b)
#define ST first
#define ND second
#define SIZE(x) (int)x.size()

const int N = 210 * 1010;
char s1[N], s2[N];
int n, m;

LL wall[N];
LL nwall[N];
LL top[N];
LL row[N], pprev[N];
const LL INF = (LL)N * N;
const LL c1 = N;

int main(){
  int id = MyNodeId();
  int num_instances = NumberOfNodes();
  /*
  n = 5000;
  m = 100000;
  REP(i,n) s1[i] = rand()%26 + 'a';
  REP(i,m) s2[i] = rand()%26 + 'a';
  */
  scanf("%d %d %s %s", &n, &m, s1, s2);
  num_instances = min(num_instances, n+1);
  if (id >= num_instances) return 0;

  int piece_row = max(100, (n+1 + num_instances-1) / num_instances);

  num_instances = min(num_instances, (n + 1 + piece_row - 1) / piece_row);
  if (id >= num_instances) return 0;

  int piece_col = max(1, (m+1)/num_instances);

  int my_first = id * piece_row;
  int my_last = min(n, (id+1) * piece_row - 1);

  fprintf(stderr, "id=%d num_instances=%d my_first=%d my_last=%d piece_col=%d\n", id, num_instances, my_first, my_last, piece_col);

  FOR(i,my_first,my_last) wall[i] = INF;

  int j = 0;
  while (j <= m) {
    int nj = min(m+1,j + piece_col); // cols in [j,nj)
    // get a piece of row above my_first
    if (id > 0) {
      Receive(id-1);
      for (int k = j; k < nj; ++k) top[k] = GetLL(id-1);
    } else {
      for (int k = j; k < nj; ++k) top[k] = INF;
    }
    // compute part of matrix
    FOR(i,my_first,my_last) {
      //compute row i
      if (i == my_first) {
        memcpy(pprev+j, top+j, (nj - j) * sizeof(LL));
      } else {
        memcpy(pprev+j, row+j, (nj - j) * sizeof(LL));
      }
      if (j > 0) {
        //TODO check
        if (i > my_first) {
          pprev[j-1] = wall[i-1]; 
        } else {
          pprev[j-1] = top[j-1];
        }
      }

      if (i == 0) {
        for (int k = j; k < nj; ++k) {
          if (i == 0 && k == 0) row[k] = 0;
          else {
            row[k] = pprev[k] + c1;
            row[k] = min(row[k], (k > j ? row[k-1] : wall[i]) + c1);
            if (i && k) {
              if (s1[i-1] == s2[k-1]) row[k] = min(row[k], pprev[k-1]); 
              else row[k] = min(row[k], pprev[k-1] + c1 + (s1[i-1] < s2[k-1]));
            }
          }
        }
      } else {
        if (j == 0) {
          for (int k = j; k < nj; ++k) {
            row[k] = pprev[k] + c1;
            row[k] = min(row[k], (k > j ? row[k-1] : wall[i]) + c1);
            if (k) {
              if (s1[i-1] == s2[k-1]) row[k] = min(row[k], pprev[k-1]); 
              else row[k] = min(row[k], pprev[k-1] + c1 + (s1[i-1] < s2[k-1]));
            }
          }
        } else {
          for (int k = j; k < nj; ++k) {
            row[k] = pprev[k] + c1;
            row[k] = min(row[k], (k > j ? row[k-1] : wall[i]) + c1);
            if (s1[i-1] == s2[k-1]) row[k] = min(row[k], pprev[k-1]); 
            else row[k] = min(row[k], pprev[k-1] + c1 + (s1[i-1] < s2[k-1]));
          }
        }
      }

      nwall[i] = row[nj-1];
    }
    memcpy(wall+my_first, nwall+my_first, sizeof(LL) * (my_last - my_first+1));

    // send piece of my_last row
    if (id < num_instances-1) {
      for (int k = j; k < nj; ++k) PutLL(id+1, row[k]);
      Send(id+1);
    } else {
      if (nj-1 == m) {
        LL res = row[m]; 
        printf("%lld %lld\n", res/N, res%N);
      }
    }
    j = nj;
  }

  return 0;
}