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
192
193
194
195
196
197
#include<algorithm>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#define FOR(i,n) for(int i = 0;i<n;++i)
#define FORI(i,b,n) for(int i = b;i<n;++i)
#define FORD(i,n) for(int i = n;i>=0;--i)
#define ZERO 0.000001
#define MAX ((1<<31)-1)
#define qprintf debug && printf
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define ull long long int
#include "message.h"
#include <unistd.h>
using namespace std;
//int debug=0;
//#define dcout usleep(myId * 1000);debug && cerr<<"["<<myId<<"] "
//#define docout debug && !myId && cerr
class F
{
	public:
		int operations, disgusts;
};
/*ostream& operator<<(ostream& os, const F r)
{
	os<<"["<<r.operations<<"->"<<r.disgusts<<"]";
	return os;
}
int myId = MyNodeId();*/
void processPackage(char*verticalText, char* horizontalText, int height, int width, F*verticalData, F*horizontalData, int magicBit){//verticalData includes a diamond
	//int debug=0;//myId==0;
	//dcout<<"Processing: height:"<<height<<", width:"<<width<<", verticalText: "<<verticalText<<", horizontalText: "<<horizontalText<<endl;
	//FOR(i,width){
		//dcout<<horizontalData[i]<<" ";
	//}
	//dcout<<endl;
	//FOR(i,height+1){
		//dcout<<verticalData[i]<<" ";
	//}
	//dcout<<endl;
	F*verticalDataOut=verticalData;
	F*verticalWorkingRow=new F[height+1];//fuck destructors!
	F fromTop,fromLeft,fromCorner;
	FOR(i,width){
		//if(i){
			//verticalData[0]=horizontalData[i];
		//}
		verticalWorkingRow[0]=horizontalData[i];
		FOR(j,height){
			//dcout<<"Computing for ["<<j<<","<<i<<"]";

			fromTop.operations = verticalWorkingRow[j].operations+1;
			fromTop.disgusts = verticalWorkingRow[j].disgusts;

			fromLeft.operations = verticalData[j+1].operations+1;
			fromLeft.disgusts = verticalData[j+1].disgusts;

			int isExchange = verticalText[j]!=horizontalText[i];
			int isDisgustingExchange = 0;
			if(isExchange && ((verticalText[j]>horizontalText[i]) ^ magicBit)){
				//dcout<<" ----------- I try to replace "<<verticalText[j]<<" with "<<horizontalText[i]<<" at ["<<j<<","<<i<<"]"<<" --------- ";
				isDisgustingExchange=1;
			}
			fromCorner.operations = verticalData[j].operations+isExchange;
			fromCorner.disgusts = verticalData[j].disgusts+isDisgustingExchange;

			//dcout<<fromTop<<","<<fromCorner<<","<<fromLeft<<" -> ";
			F bestFrom=fromLeft;
			if(fromCorner.operations==bestFrom.operations)
				bestFrom.disgusts = min(fromCorner.disgusts, bestFrom.disgusts);
			
			if(fromCorner.operations<bestFrom.operations)
				bestFrom=fromCorner;

			if(fromTop.operations==bestFrom.operations)
				bestFrom.disgusts = min(fromTop.disgusts, bestFrom.disgusts);
			
			if(fromTop.operations<bestFrom.operations)
				bestFrom=fromTop;

			verticalWorkingRow[j+1]=bestFrom;
			//dcout<<bestFrom<<endl;
		}
		horizontalData[i]=verticalWorkingRow[height];
		swap(verticalWorkingRow,verticalData);
	}
	FOR(i,height+1)verticalDataOut[i]=verticalData[i];

	//FOR(i,width){
		//dcout<<horizontalData[i]<<" ";
	//}
	//dcout<<endl;
	//FOR(i,height+1){
		//dcout<<verticalData[i]<<" ";
	//}
	//dcout<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);

	int nodes=NumberOfNodes();
	int myId = MyNodeId();
	//docout<<"Start the job in "<< nodes<<" nodes"<<endl;
	
	int verticalLength,horizontalLength;
	cin>>horizontalLength>>verticalLength;
	char *verticalText = new char[verticalLength+10];
	char *horizontalText = new char[horizontalLength+10];
	cin>>horizontalText>>verticalText;

	//docout<<"Vertical Text of length "<<verticalLength<<": "<<verticalText<<endl;
	//docout<<"Horizontal Text of length "<<horizontalLength<<": "<<horizontalText<<endl;
	int magicBit=0;
	if(verticalLength<horizontalLength){
		swap(verticalLength,horizontalLength);
		swap(verticalText,horizontalText);
		magicBit^=1;
	}
	//docout<<"Vertical Text of length "<<verticalLength<<": "<<verticalText<<endl;
	//docout<<"Horizontal Text of length "<<horizontalLength<<": "<<horizontalText<<endl;

	int horizontalStep=max(50,horizontalLength/nodes/4);
	int verticalStep=max(50,verticalLength/nodes+2);

	//docout<<"Basic vertical step  : "<<verticalStep<<endl;
	//docout<<"Basic horizontal step: "<<horizontalStep<<endl;

	int myVerticalStart=min(verticalStep*myId, verticalLength);
	int myVerticalEnd=min(verticalStep*(myId+1), verticalLength);
	int myVerticalStep=myVerticalEnd-myVerticalStart;
	//dcout<<"My verticalStart is "<<myVerticalStart<<", my verticalEnd is "<<myVerticalEnd<<" and my verticalStep is "<<myVerticalStep <<endl;

	if(myVerticalStart==myVerticalEnd)return 0;

	int currentHorizontalStep;
	int currentVerticalPosition=myVerticalStart;

	F*verticalData=new F[myVerticalStep+1];
	F*receivedAndToSend=new F[horizontalStep];

	//initiate vertical line
	FOR(i,myVerticalStep+1){
		verticalData[i].operations=myVerticalStart+i;
		verticalData[i].disgusts=0;
	}

	//FOR(i,myVerticalStep+1){
		//dcout<<verticalData[i]<<endl;
	//}

	for(int currentHorizontalPosition=0;currentHorizontalPosition<horizontalLength;currentHorizontalPosition+=horizontalStep){
		currentHorizontalStep=min(horizontalStep, horizontalLength-currentHorizontalPosition);
		//dcout<<"My current horizontalStep is "<<currentHorizontalStep<<endl;
		//initiate received
		if(myId==0){
			//jestem pierwszy
			FOR(i,currentHorizontalStep){
				receivedAndToSend[i].operations = currentHorizontalPosition + i + 1;
				receivedAndToSend[i].disgusts = 0;
			}
		}else{
			int getDataFromNode=myId-1;
			Receive(getDataFromNode);
			FOR(i,currentHorizontalStep){
				receivedAndToSend[i].operations = GetInt(getDataFromNode);
				receivedAndToSend[i].disgusts = GetInt(getDataFromNode);
			}
		}
		//FOR(i,currentHorizontalStep){
			//dcout<<"Received: "<<receivedAndToSend[i]<<endl;
		//}

		processPackage(verticalText + currentVerticalPosition, horizontalText + currentHorizontalPosition, myVerticalStep, currentHorizontalStep, verticalData, receivedAndToSend, magicBit);

		//FOR(i,currentHorizontalStep){
			//dcout<<"Sending: "<<receivedAndToSend[i]<<endl;
		//}

		if(myVerticalEnd!=verticalLength){
			int sendDataToNode=myId+1;
			FOR(i,currentHorizontalStep){
				PutInt(sendDataToNode, receivedAndToSend[i].operations);
				PutInt(sendDataToNode, receivedAndToSend[i].disgusts);
			}
			Send(sendDataToNode);
		}
	}
	if(myVerticalEnd==verticalLength){
		//I am the last one
		F toEcho=receivedAndToSend[currentHorizontalStep-1];
		cout<<toEcho.operations<<" "<<toEcho.disgusts<<endl;
	}
	
	return 0;
}