#include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #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,n) for(int i=0; i<(n); ++i) #define VAR(v,i) __typeof(i) v = (i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; template<class C> void mini(C&a4, C b4) { a4 = min(a4,b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4,b4); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; const int P = 1e2+1; const int N = 1e5+1; int numProc, myNum; int n, m; char str1[N], str2[N]; PII rowH[N]; PII rowL[N]; int colD, rowD; int colStep[P], rowStep[P*2], rowSteps; PII *last, *curr, col1[N], col2[N]; bool received[P*2]; int rowBase; PII& gCurr(int k) { return curr[k-rowBase]; } PII& gLast(int k) { return last[k-rowBase]; } PII add(const PII& a, const PII& b) { return MP(a.X + b.X, a.Y + b.Y); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); scanf("%d %d", &n, &m); scanf(" %s %s", str1, str2); numProc = NumberOfNodes(); myNum = MyNodeId(); rowSteps = min(numProc * 2, n); rowD = (n + rowSteps - 1) / rowSteps; colD = (m + numProc - 1) / numProc; rowStep[0] = 0; REP(i, rowSteps) rowStep[i+1] = min(rowStep[i] + rowD, n); colStep[0] = 0; REP(i, numProc) colStep[i+1] = min(colStep[i] + colD, m); curr = col1; last = col2; rowBase = colStep[myNum]; REP(i, rowSteps) received[i] = false; FOR(j, colStep[myNum], colStep[myNum+1]) gCurr(j) = PII(j, 0); REP(k, rowSteps) { // printf("k: %d\n", k); if (myNum == 0) { FOR(i, rowStep[k]+1, rowStep[k+1]) { rowH[i] = PII(i, 0); } } else { while (!received[k]) { Receive(myNum-1); int l = GetInt(myNum-1); received[l] = true; FOR(i, rowStep[l]+1, rowStep[l+1]) { rowH[i].X = GetInt(myNum-1); rowH[i].Y = GetInt(myNum-1); } } } FOR(i, rowStep[k]+1, rowStep[k+1]) { swap(curr, last); gCurr(colStep[myNum]) = rowH[i]; FOR(j, colStep[myNum]+1, colStep[myNum+1]) { PII best = add(min(gCurr(j-1), gLast(j)), MP(1,0)); if (str1[i-1] == str2[j-1]) { mini(best, gLast(j-1)); } else if (str1[i-1] < str2[j-1]) { mini(best, add(gLast(j-1), MP(1,1))); } else { mini(best, add(gLast(j-1), MP(1,0))); } gCurr(j) = best; // printf("%d x %d: %d %d\n", i, j, gCurr(j).X, gCurr(j).Y); } rowL[i] = gCurr(colStep[myNum+1]); } if (myNum < numProc-1) { PutInt(myNum+1, k); FOR(i, rowStep[k]+1, rowStep[k+1]) { PutInt(myNum+1, rowL[i].X); PutInt(myNum+1, rowL[i].Y); }; Send(myNum+1); } } if (myNum == numProc-1) { printf("%d %d\n", gCurr(m).X, gCurr(m).Y); } 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 141 142 143 144 145 146 147 | #include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #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,n) for(int i=0; i<(n); ++i) #define VAR(v,i) __typeof(i) v = (i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; template<class C> void mini(C&a4, C b4) { a4 = min(a4,b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4,b4); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; const int P = 1e2+1; const int N = 1e5+1; int numProc, myNum; int n, m; char str1[N], str2[N]; PII rowH[N]; PII rowL[N]; int colD, rowD; int colStep[P], rowStep[P*2], rowSteps; PII *last, *curr, col1[N], col2[N]; bool received[P*2]; int rowBase; PII& gCurr(int k) { return curr[k-rowBase]; } PII& gLast(int k) { return last[k-rowBase]; } PII add(const PII& a, const PII& b) { return MP(a.X + b.X, a.Y + b.Y); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); scanf("%d %d", &n, &m); scanf(" %s %s", str1, str2); numProc = NumberOfNodes(); myNum = MyNodeId(); rowSteps = min(numProc * 2, n); rowD = (n + rowSteps - 1) / rowSteps; colD = (m + numProc - 1) / numProc; rowStep[0] = 0; REP(i, rowSteps) rowStep[i+1] = min(rowStep[i] + rowD, n); colStep[0] = 0; REP(i, numProc) colStep[i+1] = min(colStep[i] + colD, m); curr = col1; last = col2; rowBase = colStep[myNum]; REP(i, rowSteps) received[i] = false; FOR(j, colStep[myNum], colStep[myNum+1]) gCurr(j) = PII(j, 0); REP(k, rowSteps) { // printf("k: %d\n", k); if (myNum == 0) { FOR(i, rowStep[k]+1, rowStep[k+1]) { rowH[i] = PII(i, 0); } } else { while (!received[k]) { Receive(myNum-1); int l = GetInt(myNum-1); received[l] = true; FOR(i, rowStep[l]+1, rowStep[l+1]) { rowH[i].X = GetInt(myNum-1); rowH[i].Y = GetInt(myNum-1); } } } FOR(i, rowStep[k]+1, rowStep[k+1]) { swap(curr, last); gCurr(colStep[myNum]) = rowH[i]; FOR(j, colStep[myNum]+1, colStep[myNum+1]) { PII best = add(min(gCurr(j-1), gLast(j)), MP(1,0)); if (str1[i-1] == str2[j-1]) { mini(best, gLast(j-1)); } else if (str1[i-1] < str2[j-1]) { mini(best, add(gLast(j-1), MP(1,1))); } else { mini(best, add(gLast(j-1), MP(1,0))); } gCurr(j) = best; // printf("%d x %d: %d %d\n", i, j, gCurr(j).X, gCurr(j).Y); } rowL[i] = gCurr(colStep[myNum+1]); } if (myNum < numProc-1) { PutInt(myNum+1, k); FOR(i, rowStep[k]+1, rowStep[k+1]) { PutInt(myNum+1, rowL[i].X); PutInt(myNum+1, rowL[i].Y); }; Send(myNum+1); } } if (myNum == numProc-1) { printf("%d %d\n", gCurr(m).X, gCurr(m).Y); } return 0; } |