#include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #include "message.h" #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; const int MAXN = 100005; const int MAXMSG = 1000; const int LINES_PER_PROC = 1000; int Procs, ProcID; int n, m, K; char a[MAXN], b[MAXN]; int begIdx, endIdx; PII dp[2][MAXN]; void setParams() { Procs = min(Procs, (m + LINES_PER_PROC - 1) / LINES_PER_PROC); if (ProcID >= Procs) { exit(0); } K = 1; while (K * MAXMSG < n) { ++K; } K = max(K, m / Procs); begIdx = (ProcID * m) / Procs; endIdx = ((ProcID + 1) * m) / Procs; } int main() { scanf("%d %d", &n, &m); if (n >= m) { scanf("%s", a); scanf("%s", b); } else { swap(n,m); scanf("%s", b); scanf("%s", a); REP (i, n) { a[i] = ('z'-a[i]) + 'a'; } REP (i, m) { b[i] = ('z'-b[i]) + 'a'; } } Procs = NumberOfNodes(); ProcID = MyNodeId(); setParams(); int prev = 0, curr = 1; FOR (i, begIdx, endIdx + 1) { dp[prev][i] = {i, 0}; } PII p1, p2, p3; FOR (i, 1, n+1) { if (ProcID == 0) { dp[curr][0] = {i, 0}; } else { if ((i-1) % K == 0) { Receive(ProcID-1); } dp[curr][begIdx].ST = GetInt(ProcID-1); dp[curr][begIdx].ND = GetInt(ProcID-1); } FOR (j, begIdx+1, endIdx+1) { if (a[i-1] == b[j-1]) { dp[curr][j] = dp[prev][j-1]; } else { p1 = {dp[curr][j-1].ST + 1, dp[curr][j-1].ND}; p2 = {dp[prev][j].ST + 1, dp[prev][j].ND}; p3 = {dp[prev][j-1].ST + 1, dp[prev][j-1].ND + (a[i-1] < b[j-1])}; dp[curr][j] = min(p1, min(p2, p3)); } } if (ProcID != Procs - 1) { PutInt(ProcID+1, dp[curr][endIdx].ST); PutInt(ProcID+1, dp[curr][endIdx].ND); if (i % K == 0 || i == n) { Send(ProcID+1); } } swap(prev, curr); } if (endIdx == m) { printf("%d %d\n", dp[prev][m].ST, dp[prev][m].ND); } 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 | #include <vector> #include <list> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #include "message.h" #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define REP(i,a) FOR(i,0,a) #define MP make_pair #define PB push_back #define ST first #define ND second using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef pair<int, int> PII; typedef vector<PII> VII; typedef long long int LL; typedef unsigned long long int ULL; const int MAXN = 100005; const int MAXMSG = 1000; const int LINES_PER_PROC = 1000; int Procs, ProcID; int n, m, K; char a[MAXN], b[MAXN]; int begIdx, endIdx; PII dp[2][MAXN]; void setParams() { Procs = min(Procs, (m + LINES_PER_PROC - 1) / LINES_PER_PROC); if (ProcID >= Procs) { exit(0); } K = 1; while (K * MAXMSG < n) { ++K; } K = max(K, m / Procs); begIdx = (ProcID * m) / Procs; endIdx = ((ProcID + 1) * m) / Procs; } int main() { scanf("%d %d", &n, &m); if (n >= m) { scanf("%s", a); scanf("%s", b); } else { swap(n,m); scanf("%s", b); scanf("%s", a); REP (i, n) { a[i] = ('z'-a[i]) + 'a'; } REP (i, m) { b[i] = ('z'-b[i]) + 'a'; } } Procs = NumberOfNodes(); ProcID = MyNodeId(); setParams(); int prev = 0, curr = 1; FOR (i, begIdx, endIdx + 1) { dp[prev][i] = {i, 0}; } PII p1, p2, p3; FOR (i, 1, n+1) { if (ProcID == 0) { dp[curr][0] = {i, 0}; } else { if ((i-1) % K == 0) { Receive(ProcID-1); } dp[curr][begIdx].ST = GetInt(ProcID-1); dp[curr][begIdx].ND = GetInt(ProcID-1); } FOR (j, begIdx+1, endIdx+1) { if (a[i-1] == b[j-1]) { dp[curr][j] = dp[prev][j-1]; } else { p1 = {dp[curr][j-1].ST + 1, dp[curr][j-1].ND}; p2 = {dp[prev][j].ST + 1, dp[prev][j].ND}; p3 = {dp[prev][j-1].ST + 1, dp[prev][j-1].ND + (a[i-1] < b[j-1])}; dp[curr][j] = min(p1, min(p2, p3)); } } if (ProcID != Procs - 1) { PutInt(ProcID+1, dp[curr][endIdx].ST); PutInt(ProcID+1, dp[curr][endIdx].ND); if (i % K == 0 || i == n) { Send(ProcID+1); } } swap(prev, curr); } if (endIdx == m) { printf("%d %d\n", dp[prev][m].ST, dp[prev][m].ND); } return 0; } |