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
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#else
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <algorithm>
#include <string>
#include <iostream>
#include "message.h"
#define FOR(x,y,z) for (int x=(y); x<=(z); ++x)
#define FORD(x,y,z) for(int x=(y); x>=(z); --x)
#define REP(x,y) for (int x=0; x<(y); ++x)
#if defined(__GNUC__) && __cplusplus < 201103L
#define FOREACH(y,x) for (typeof((x).begin()) y = (x).begin(); y != (x).end(); ++y)
#else
#define FOREACH(y,x) for (auto y = (x).begin(); y != (x).end(); ++y)
#endif
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((int)(x).size())
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
const int INF = 1000000001;

int main(int argc, char** argv)
{
	int nodes = NumberOfNodes();
	int id = MyNodeId();
	int n,m;
	scanf("%d%d", &n, &m);
	vector<char> tmp(max(n,m)+1);
	scanf("%s", &tmp[0]);
	string s1(&tmp[0]);
	scanf("%s", &tmp[0]);
	string s2(&tmp[0]);
	int nn = (n+nodes-1)/nodes*nodes;
	int mm = (m+nodes-1)/nodes*nodes;
	s1.append(nn-n, 'a');
	s2.append(mm-m, 'a');
	int u = nn/nodes;
	int v = mm/nodes;
	int a = id, b = 0;
	VPII t[2];
	REP(i,2) t[i].resize(u+1);
	int tix = 1;	
	VPII col(v+1);
	FOR(i,0,v) col[i] = PII(a*v+i,0);
	bool out = false;
	PII res;
	REP(z,2*nodes-2*id-1) {
		if (a != 0) {
			if (z < nodes-id) {
				Receive(id-1);
				FOR(i,1,u) {
					int x = GetInt(id-1);
					int y = GetInt(id-1);
					t[0][i] = PII(x,y);
				}
				t[0][0] = col[0];
				col[0] = t[0][u];
				tix = 1;
			}
			else {
				Receive(id+1);
				FOR(i,1,v) {
					int x = GetInt(id+1);
					int y = GetInt(id+1);
					col[i] = PII(x,y);
				}
			}
		}
		else {
			FOR(i,0,u) t[0][i] = PII(b*u+i,0);
			tix = 1;
		}
		int s1ix = b*u, s2ix = a*v;
		FOR(i,1,v) {
			t[tix][0] = col[i];
			FOR(j,1,u) {
				PII x1 = t[tix][j-1], x2 = t[1-tix][j];
				PII x = min(PII(x1.first+1,x1.second), PII(x2.first+1,x2.second));
				char c2 = s2[s2ix+i-1], c1 = s1[s1ix+j-1];
				PII y = t[1-tix][j-1];
				if (c1 == c2) x = min(x, y);
				else if (c2 > c1) x = min(x, PII(y.first+1, y.second+1));
				else x = min(x, PII(y.first+1, y.second));
				t[tix][j] = x;
				if (s2ix+i == m && s1ix+j == n) {
					out = true;
					res = x;
				}
			}
			col[i] = t[tix][u];
			tix = 1-tix;
		}
		if (z < nodes-id-1) {
			FOR(i,1,u) {
				PutInt(id+1, t[1-tix][i].first);
				PutInt(id+1, t[1-tix][i].second);
			}
			Send(id+1);
			++b;
		}
		else {
			if (id-1 >= 0) {
				FOR(i,1,v) {
					PutInt(id-1, col[i].first);
					PutInt(id-1, col[i].second);
				}
				Send(id-1);
			}
			++a;
		}
		
	}
	if (out) printf("%d %d\n", res.first, res.second);
	return 0;
}