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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>

#include <time.h>
#include "message.h"

#define SC(n) scanf("%d", &n)
#define SC2(n, m) scanf("%d %d", &n, &m)
#define SCC(c) scanf(" %c", &c)
#define SCS(b) scanf("%s", b)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FORE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define FORIT(i, c, typ) for(typ::iterator i = c.begin(); i != c.end(); ++i)
#define PR(n) printf("%d ", (int) (n)) 
#define PRN(n) printf("%d\n", (int) (n)) 
#define elif else if
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define all(v) v.begin(), v.end()
#define itr iterator
#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int, pi> pii;
typedef vector<pii> vpii;
typedef vector<vpi> vvpi;
typedef map<int, int> mi;

const int NMAX = 1000*100 + 7, HMIN = 100, WMIN = 50, LEVMAX = 996;

char s1[NMAX], s2[NMAX], c;
int n, m, k, id, imOut, h, w, h1, h2, w1, w2, change;
int t[NMAX][2], z[NMAX][2], lz[NMAX], lt[NMAX];

inline void update(int tr1, int tr0, int wi, int hr, int wr) {
	if(s1[wr] == s2[hr]) {
		t[wi][tr1] = t[wi - 1][tr0];
		z[wi][tr1] = z[wi - 1][tr0];
	}
	else {
		t[wi][tr1] = t[wi - 1][tr0] + 1;
		z[wi][tr1] = z[wi - 1][tr0] + ((s1[wr] < s2[hr]) == change);
		
		if(t[wi - 1][tr1] + 1 < t[wi][tr1]) {
			t[wi][tr1] = t[wi - 1][tr1] + 1;
			z[wi][tr1] = z[wi - 1][tr1];
		}
		else if(t[wi - 1][tr1] + 1 == t[wi][tr1] && z[wi - 1][tr1] < z[wi][tr1]) {
			z[wi][tr1] = z[wi - 1][tr1];
		}
		
		if(t[wi][tr0] + 1 < t[wi][tr1]) {
			t[wi][tr1] = t[wi][tr0] + 1;
			z[wi][tr1] = z[wi][tr0];
		}
		else if(t[wi][tr0] + 1 == t[wi][tr1] && z[wi][tr0] < z[wi][tr1]) {
			z[wi][tr1] = z[wi][tr0];
		}
	}
	dbg FORE(i, 1, wr) prd("%c", s1[i]);
	prd(" ");
	dbg FORE(i, 1, hr) prd("%c", s2[i]);
	prd(" ");
	if(t[wi][tr1] == t[wi -1][tr0] + 1) prd("(%d + %d) ", z[wi - 1][tr0], (s1[wr] < s2[hr]) == change);
	prd("tz[%d, %d] = %d %d\n", hr, wr, t[wi][tr1], z[wi][tr1]);
}

void countBlock(int w1, int w2, int h1, int h2) {
	prd("%d: count [%d, %d] [%d, %d]\n", id, w1, w2, h1, h2);
	int tr1 = 1, tr0 = 0;
	
	FORE(hi, h1, h2) {
		t[w1 - 1][tr1] = lt[hi];
		z[w1 - 1][tr1] = lz[hi];
		
		FORE(wi, w1, w2)
			update(tr1, tr0, wi, hi, wi); // TODO usun 3. argument
		if(w2 < n) {
			PutInt(id + 1, t[w2][tr1]);
			PutInt(id + 1, z[w2][tr1]);
		}
		tr1 = 1 - tr1;
		tr0 = 1 - tr0;
	}
}

int main() {
	k = NumberOfNodes();
	id = MyNodeId();
	SC2(n, m);
	if(n <= m) { // n <= m
		REP(i, n)
			SCC(s1[i+1]);
		REP(i, m)
			SCC(s2[i+1]);
		change = 1;
	}
	else {
		int tmp = n;
		n = m;
		m = tmp;
		REP(i, m)
			SCC(s2[i+1]);
		REP(i, n)
			SCC(s1[i+1]);
		change = 0;
	}
	
	dbg FORE(i, 1, n) prd("%c", s1[i]);
	prd("\n");
	dbg FORE(i, 1, m) prd("%c", s2[i]);
	prd("\n");
	
	w = max(WMIN, (n + k - 1) / k);
	if(w * id >= n)
		imOut = 1;
	h = max(HMIN, (m + LEVMAX - 1)/ LEVMAX);
	if(h&1) // h parzyste
		++h;
	
	if(imOut)
		return 0;
	
	w1 = id * w + 1;
	w2 = (id + 1) * w;
	h1 = 1;
	h2 = h;
	
	while(h1 <= m) {
		w2 = min(w2, n);
		h2 = min(h2, m);
		
		if(h1 == 1) {
			FORE(i, w1 - 1, w2)
				t[i][0] = i;
			if(w2 < n) {
				PutInt(id + 1, t[w2][0]);
				PutInt(id + 1, z[w2][0]);	
			}
		}
		
		if(id == 0)
			FORE(i, h1 - 1, h2)
				lt[i] = i;
		else {
			Receive(id - 1);
			FORE(i, h1 - 1, h2) {
				lt[i] = GetInt(id - 1);
				lz[i] = GetInt(id - 1);
			}
			t[w1 - 1][0] = lt[h1 - 1];
			z[w1 - 1][0] = lz[h1 - 1];
		}
		
		countBlock(w1, w2, h1, h2);
		if(w2 < n) {
			Send(id + 1);
			PutInt(id + 1, t[w2][0]);
			PutInt(id + 1, z[w2][0]);	
		}
			
		if(w2 == n && h2 == m)
			printf("%d %d", t[n][m & 1], z[n][m & 1]);
		
		h1 += h;
		h2 += h;
	}

	return 0;
}