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
#include "message.h"
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

#define BLOCK 1000

struct Cell{
	int cost, shock;
	
	Cell() : cost(0), shock(0) {}
	Cell(int c, int sh) : cost(c), shock(sh) {}
};

void print(vector<Cell> &vec){
	for(int i=0; i<(int)vec.size(); i++)
		printf("%d %d | ", vec[i].cost, vec[i].shock);
	
	printf("\n");
}

int main(){
	int n, m;
	
	scanf("%d%d", &m, &n);
	
	int id = MyNodeId();
	int nodes = NumberOfNodes();
	int arrSize = m + 1;
	int rangeSize = arrSize / nodes;
	if(rangeSize * nodes < arrSize) rangeSize++;
	int start = id * rangeSize;
	int end = min(start + rangeSize, arrSize);
	bool empty = (end <= start);
	bool last = (!empty && end == arrSize);
	
	if(empty) return 0;
	
	char *mStr = new char[m + 5];
	char *nStr = new char[n + 5];
	
	scanf("%s", mStr + 1);
	scanf("%s", nStr + 1);
	
	vector<Cell> vec1, vec2, firstColumn, lastColumn;
	vector<Cell> &prev = vec1;
	vector<Cell> &next = vec2;
	vector<Cell> &temp = vec1;
	
	firstColumn.push_back(Cell(start, 0));
	lastColumn.push_back(Cell(end - 1, 0));
	
	if(id > 0){
		prev.push_back(Cell(start - 1, 0));
		next.push_back(Cell());
	}
	
	for(int i=start; i<end; i++){
		prev.push_back(Cell(i, 0));
		next.push_back(Cell());
	}
	
	if(id == 0){
		for(int j=1; j<=n; j++)
			firstColumn.push_back(Cell(j, 0));
	}
	
	for(int j=1; j<=n; j++){
		if(j % BLOCK == 1){	
			if(id > 0){
				Receive(id - 1);
				int toGet = GetInt(id - 1);
		
				for(int k=0; k<toGet; k++){
					int cost = GetInt(id - 1);
					int shock = GetInt(id - 1);
					firstColumn.push_back(Cell(cost, shock));
				}
			}
		}
		
		//  ------- calculate --------------
		
		int len = prev.size();
		
		next[0] = firstColumn[j];
		
		int mStrPlus = (id == 0 ? start : start - 1);
		
		for(int i=1; i<len; i++){
			Cell &nexti = next[i];
			Cell &previ = prev[i];
			Cell &nexti1 = next[i-1];
			Cell &previ1 = prev[i-1];
			
			int del = nexti1.cost + 1; // ops[i-1][j] + 1;
			int ins = previ.cost + 1; // ops[i][j-1] + 1;
			int sub = previ1.cost + (mStr[i + mStrPlus] == nStr[j] ? 0 : 1); // ops[i-1][j-1] + (mStr[i] == nStr[j] ? 0 : 1);
			int minOps = min(min(del, ins), sub);
				
			nexti.cost = minOps;
			nexti.shock = 99999999;
				
			if(del == minOps)
				nexti.shock = min(nexti.shock, nexti1.shock); // min(subs[i][j], subs[i-1][j]);
				
			if(ins == minOps)
				nexti.shock = min(nexti.shock, previ.shock); // min(subs[i][j], subs[i][j-1]);
					
			if(sub == minOps)
				nexti.shock = min(nexti.shock, previ1.shock + (mStr[i + mStrPlus] < nStr[j] ? 1 : 0)); 
					// min(subs[i][j], subs[i-1][j-1] + (mStr[i] < nStr[j] ? 1 : 0));
		}
		
		// ---------------------------------
		
		lastColumn.push_back(next.back());
		
		/*if(id == 0){
			print(prev);
			print(next);
		}*/
		
		temp = prev;
		prev = next;
		next = temp;
		
		if(j % BLOCK == 0 || j == n){
			if(!last){
				int toSend = (j % BLOCK == 0 ? BLOCK : j % BLOCK);
				PutInt(id + 1, toSend);
			
				for(int k=1; k<=toSend; k++){
					PutInt(id + 1, lastColumn[j-toSend+k].cost);
					PutInt(id + 1, lastColumn[j-toSend+k].shock);
				}
				
				Send(id + 1);
			}
		}
	}
	
	if(last)
		printf("%d %d\n", prev.back().cost, prev.back().shock);
	
	delete [] nStr;
	delete [] mStr;

	return 0;
}