#include <cstdio> #include "message.h" #define MAXN 100007 #define MAXMSG 900 #define DEBUG(...) //#define DEBUG(...) printf(__VA_ARGS__); fflush(stdout) int NODES, MYID; bool inv; int n1, n2; char str1[MAXN], str2[MAXN]; int leftdst[MAXN]; int leftszok[MAXN]; int mindst[MAXN][2]; int minszok[MAXN][2]; int startx[MAXN], numx; int starty[MAXN], numy; bool cur; inline bool szok(char from, char to) { return (from < to) ^ inv; } void solve(int x1, int x2, int y1, int y2) { int i, x, y, X = x2 - x1, Y = y2 - y1; DEBUG("SOLVE(%d): [%d,%d]x[%d,%d]\n", MYID, x1, x2, y1, y2); for (y = y1; y < y2; y++) { mindst[x1][cur] = leftdst[y+1]; minszok[x1][cur] = leftszok[y+1]; for (x = x1; x < x2; x++) { #define CUR(a) a[x+1][cur] #define DIAG(a) a[x][!cur] #define LEFT(a) a[x][cur] #define TOP(a) a[x+1][!cur] CUR(mindst) = DIAG(mindst); CUR(minszok) = DIAG(minszok); if (str1[x] == str2[y]) { DEBUG("%c%c(%2d %2d) ", str1[x], str2[y], CUR(mindst), CUR(minszok)); continue; /* same. */ } CUR(minszok) += szok(str1[x], str2[y]); if (LEFT(mindst) < CUR(mindst) || (LEFT(mindst) == CUR(mindst) && LEFT(minszok) < CUR(minszok))) { CUR(mindst) = LEFT(mindst); CUR(minszok) = LEFT(minszok); } if (TOP(mindst) < CUR(mindst) || (TOP(mindst) == CUR(mindst) && TOP(minszok) < CUR(minszok))) { CUR(mindst) = TOP(mindst); CUR(minszok) = TOP(minszok); } CUR(mindst)++; DEBUG("%c%c(%2d %2d) ", str1[x], str2[y], CUR(mindst), CUR(minszok)); } DEBUG("\n"); leftdst[y+1] = mindst[x2][cur]; leftszok[y+1] = minszok[x2][cur]; cur = !cur; } } void receive_up(int i) { int x; if (MYID == 0) { /* initialize top edge. */ for (x = startx[i]; x <= startx[i+1]; x++) { mindst[x][!cur] = x; } } else { /* receive top edge from previous node. */ Receive(MYID-1); for (x = startx[i]; x <= startx[i+1]; x++) { mindst[x][!cur] = GetInt(MYID-1); minszok[x][!cur] = GetInt(MYID-1); } } } void send_down(int i) { int x; if (MYID == NODES-1) return; for (x = startx[i]; x <= startx[i+1]; x++) { PutInt(MYID+1, mindst[x][!cur]); PutInt(MYID+1, minszok[x][!cur]); } Send(MYID+1); } void run() { int i; /* initialize left edge. */ for (i = starty[MYID]; i <= starty[MYID+1]; i++) { leftdst[i] = i; } for (i = 0; i < numx; i++) { receive_up(i); solve(startx[i], startx[i+1], starty[MYID], starty[MYID+1]); send_down(i); } } int main() { NODES = NumberOfNodes(); MYID = MyNodeId(); scanf("%d %d", &n1, &n2); if (n1 >= n2) { scanf("%s", str1); scanf("%s", str2); inv = false; } else { int tmp = n1; n1 = n2; n2 = tmp; scanf("%s", str2); scanf("%s", str1); inv = true; } /* Each machine processes at least 1 row. */ if (NODES > n2) { if (MYID >= n2) return 0; NODES = n2; } numy = NODES; numx = MAXMSG; if (n1 / numx < 5) numx = 1 + n1 / 5; for (int i = 0; i <= numx; i++) startx[i] = (long long) (i * n1) / numx; for (int i = 0; i <= numy; i++) starty[i] = (long long) (i * n2) / numy; run(); if (MYID == NODES-1) { printf("%d %d\n", mindst[n1][!cur], minszok[n1][!cur]); } return 0; }
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 | #include <cstdio> #include "message.h" #define MAXN 100007 #define MAXMSG 900 #define DEBUG(...) //#define DEBUG(...) printf(__VA_ARGS__); fflush(stdout) int NODES, MYID; bool inv; int n1, n2; char str1[MAXN], str2[MAXN]; int leftdst[MAXN]; int leftszok[MAXN]; int mindst[MAXN][2]; int minszok[MAXN][2]; int startx[MAXN], numx; int starty[MAXN], numy; bool cur; inline bool szok(char from, char to) { return (from < to) ^ inv; } void solve(int x1, int x2, int y1, int y2) { int i, x, y, X = x2 - x1, Y = y2 - y1; DEBUG("SOLVE(%d): [%d,%d]x[%d,%d]\n", MYID, x1, x2, y1, y2); for (y = y1; y < y2; y++) { mindst[x1][cur] = leftdst[y+1]; minszok[x1][cur] = leftszok[y+1]; for (x = x1; x < x2; x++) { #define CUR(a) a[x+1][cur] #define DIAG(a) a[x][!cur] #define LEFT(a) a[x][cur] #define TOP(a) a[x+1][!cur] CUR(mindst) = DIAG(mindst); CUR(minszok) = DIAG(minszok); if (str1[x] == str2[y]) { DEBUG("%c%c(%2d %2d) ", str1[x], str2[y], CUR(mindst), CUR(minszok)); continue; /* same. */ } CUR(minszok) += szok(str1[x], str2[y]); if (LEFT(mindst) < CUR(mindst) || (LEFT(mindst) == CUR(mindst) && LEFT(minszok) < CUR(minszok))) { CUR(mindst) = LEFT(mindst); CUR(minszok) = LEFT(minszok); } if (TOP(mindst) < CUR(mindst) || (TOP(mindst) == CUR(mindst) && TOP(minszok) < CUR(minszok))) { CUR(mindst) = TOP(mindst); CUR(minszok) = TOP(minszok); } CUR(mindst)++; DEBUG("%c%c(%2d %2d) ", str1[x], str2[y], CUR(mindst), CUR(minszok)); } DEBUG("\n"); leftdst[y+1] = mindst[x2][cur]; leftszok[y+1] = minszok[x2][cur]; cur = !cur; } } void receive_up(int i) { int x; if (MYID == 0) { /* initialize top edge. */ for (x = startx[i]; x <= startx[i+1]; x++) { mindst[x][!cur] = x; } } else { /* receive top edge from previous node. */ Receive(MYID-1); for (x = startx[i]; x <= startx[i+1]; x++) { mindst[x][!cur] = GetInt(MYID-1); minszok[x][!cur] = GetInt(MYID-1); } } } void send_down(int i) { int x; if (MYID == NODES-1) return; for (x = startx[i]; x <= startx[i+1]; x++) { PutInt(MYID+1, mindst[x][!cur]); PutInt(MYID+1, minszok[x][!cur]); } Send(MYID+1); } void run() { int i; /* initialize left edge. */ for (i = starty[MYID]; i <= starty[MYID+1]; i++) { leftdst[i] = i; } for (i = 0; i < numx; i++) { receive_up(i); solve(startx[i], startx[i+1], starty[MYID], starty[MYID+1]); send_down(i); } } int main() { NODES = NumberOfNodes(); MYID = MyNodeId(); scanf("%d %d", &n1, &n2); if (n1 >= n2) { scanf("%s", str1); scanf("%s", str2); inv = false; } else { int tmp = n1; n1 = n2; n2 = tmp; scanf("%s", str2); scanf("%s", str1); inv = true; } /* Each machine processes at least 1 row. */ if (NODES > n2) { if (MYID >= n2) return 0; NODES = n2; } numy = NODES; numx = MAXMSG; if (n1 / numx < 5) numx = 1 + n1 / 5; for (int i = 0; i <= numx; i++) startx[i] = (long long) (i * n1) / numx; for (int i = 0; i <= numy; i++) starty[i] = (long long) (i * n2) / numy; run(); if (MYID == NODES-1) { printf("%d %d\n", mindst[n1][!cur], minszok[n1][!cur]); } return 0; } |