#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> #include "message.h" using namespace std; const int N = 100002; const int INF = 0x7fffffff; char a[N]; char b[N]; int n, m; int nn, id; pair<int,int> row[2][N]; void transformString(char *s) { for (; *s; ++s) { *s = (char) ((int)'z' - (int)*s + (int)'a'); } } void swapStrings() { char t[N]; strcpy(t, a); strcpy(a, b); strcpy(b, t); swap(n, m); transformString(a); transformString(b); } int main() { nn = NumberOfNodes(); id = MyNodeId(); int wn = min(11, nn); scanf("%d%d", &n, &m); scanf("%s%s", a, b); if (n < m) swapStrings(); if (m < 100 || n < 200) { wn = 1; } if (id >= wn) return 0; int mChunk = (m + wn - 1)/ wn; int begin = id * mChunk; int end = min(m, (id + 1) * mChunk); int mm = end - begin; for (int i = 0; i <= mm; ++i) { row[0][i].first = row[0][i].second = row[1][i].first = row[1][i].second = 0; } int nChunk = n / 100 + 10; int received = 0; int toSend = 0; for (int i = 0; i <= n; ++i) { int x = i % 2; int y = 1 - x; // receive if (id) { if (!received) { received = nChunk; Receive(id - 1); } row[x][0].first = GetInt(id - 1); row[x][0].second = GetInt(id - 1); --received; } for (int j = 1 - (id == 0); j <= mm; ++j) { if (i || j) { row[x][j] = make_pair(INF, INF); } else { row[x][j] = make_pair(0, 0); } // delete if (i) { auto t = row[y][j]; ++t.first; row[x][j] = min(row[x][j], t); } // change if (i && j) { auto t = row[y][j-1]; if (a[i-1] != b[begin + j-1]) { ++t.first; } if (a[i-1] < b[begin + j-1]) { ++t.second; } row[x][j] = min(row[x][j], t); } // insert if (j) { auto t = row[x][j-1]; ++t.first; row[x][j] = min(row[x][j], t); } //printf("[%d %d] = %d %d\n", i, j, row[x][j].first, row[x][j].second); } if (id < wn - 1) { PutInt(id + 1, row[x][mm].first); PutInt(id + 1, row[x][mm].second); ++toSend; if (toSend == nChunk || i == n) { Send(id + 1); toSend = 0; } } } if (id == wn - 1) { printf("%d %d\n", row[n % 2][mm].first, row[n % 2][mm].second); } 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 | #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <queue> #include <set> #include <map> #include <numeric> #include <utility> #include <string> #include <sstream> #include <algorithm> #include "message.h" using namespace std; const int N = 100002; const int INF = 0x7fffffff; char a[N]; char b[N]; int n, m; int nn, id; pair<int,int> row[2][N]; void transformString(char *s) { for (; *s; ++s) { *s = (char) ((int)'z' - (int)*s + (int)'a'); } } void swapStrings() { char t[N]; strcpy(t, a); strcpy(a, b); strcpy(b, t); swap(n, m); transformString(a); transformString(b); } int main() { nn = NumberOfNodes(); id = MyNodeId(); int wn = min(11, nn); scanf("%d%d", &n, &m); scanf("%s%s", a, b); if (n < m) swapStrings(); if (m < 100 || n < 200) { wn = 1; } if (id >= wn) return 0; int mChunk = (m + wn - 1)/ wn; int begin = id * mChunk; int end = min(m, (id + 1) * mChunk); int mm = end - begin; for (int i = 0; i <= mm; ++i) { row[0][i].first = row[0][i].second = row[1][i].first = row[1][i].second = 0; } int nChunk = n / 100 + 10; int received = 0; int toSend = 0; for (int i = 0; i <= n; ++i) { int x = i % 2; int y = 1 - x; // receive if (id) { if (!received) { received = nChunk; Receive(id - 1); } row[x][0].first = GetInt(id - 1); row[x][0].second = GetInt(id - 1); --received; } for (int j = 1 - (id == 0); j <= mm; ++j) { if (i || j) { row[x][j] = make_pair(INF, INF); } else { row[x][j] = make_pair(0, 0); } // delete if (i) { auto t = row[y][j]; ++t.first; row[x][j] = min(row[x][j], t); } // change if (i && j) { auto t = row[y][j-1]; if (a[i-1] != b[begin + j-1]) { ++t.first; } if (a[i-1] < b[begin + j-1]) { ++t.second; } row[x][j] = min(row[x][j], t); } // insert if (j) { auto t = row[x][j-1]; ++t.first; row[x][j] = min(row[x][j], t); } //printf("[%d %d] = %d %d\n", i, j, row[x][j].first, row[x][j].second); } if (id < wn - 1) { PutInt(id + 1, row[x][mm].first); PutInt(id + 1, row[x][mm].second); ++toSend; if (toSend == nChunk || i == n) { Send(id + 1); toSend = 0; } } } if (id == wn - 1) { printf("%d %d\n", row[n % 2][mm].first, row[n % 2][mm].second); } return 0; } |