#include "message.h" #include <cmath> #include <iostream> using namespace std; template <typename T> T min3(const T& v1, const T& v2, const T& v3) { return min(v1, min(v2, v3)); } struct Stat { int changes=0, shocks=0; Stat(int c, int s) : changes(c), shocks(s) {} Stat() {} bool operator < (const Stat& that) const { return changes!=that.changes ? changes<that.changes : shocks<that.shocks; } Stat operator + (const Stat& that) const { return Stat(changes+that.changes, shocks+that.shocks); } }; int main() { const int STEPS = 500; { int n, m; cin >> n >> m; } string s1, s2; cin >> s1 >> s2; int di = s1.size()/STEPS +1; int dj = s2.size()/NumberOfNodes() +1; int j0 = MyNodeId()*dj; if(j0>s2.size()) return 0; Stat top[di]; Stat left[dj+1], actual[dj+1]; for(int i0=0; i0<=s1.size(); i0+=di) { if(j0>0) { Receive(MyNodeId()-1); //fprintf(stderr, "%d -> I#%d\n", i0/di, MyNodeId()); for(int i=i0; i<i0+di && i<=s1.size(); i++) { int c = GetInt(MyNodeId()-1); int s = GetInt(MyNodeId()-1); top[i-i0] = Stat(c, s); } } for(int i=i0; i<i0+di && i<=s1.size(); i++) { actual[0] = top[i-i0]; for(int j=j0; j<j0+dj && j<=s2.size(); j++) { if(i==0 || j==0) actual[j-j0 +1]=Stat(max(i, j), 0); else actual[j-j0 +1] = min3( actual[(j-1)-j0 +1] + Stat(1, 0), left[j-j0 +1] +Stat(1, 0), left[(j-1)-j0 +1] + ( s1[i-1]==s2[j-1] ? Stat(0,0) : Stat(1, s1[i-1]<s2[j-1] ? 1:0) ) ); } if(MyNodeId()+1 < NumberOfNodes()) { PutInt(MyNodeId()+1, actual[dj].changes); PutInt(MyNodeId()+1, actual[dj].shocks); } for(int j=j0; j<j0+dj && j<=s2.size(); j++) { left[j-j0 +1] = actual[j-j0 +1]; } left[0] = actual[0]; } if(MyNodeId()+1 < NumberOfNodes()) { Send(MyNodeId()+1); //fprintf(stderr, "I#%d ->%d\n", MyNodeId(), i0/di); } } if(j0+dj > s2.size()) printf("%d %d\n", actual[s2.size()-j0 +1].changes, actual[s2.size()-j0 +1].shocks); 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 | #include "message.h" #include <cmath> #include <iostream> using namespace std; template <typename T> T min3(const T& v1, const T& v2, const T& v3) { return min(v1, min(v2, v3)); } struct Stat { int changes=0, shocks=0; Stat(int c, int s) : changes(c), shocks(s) {} Stat() {} bool operator < (const Stat& that) const { return changes!=that.changes ? changes<that.changes : shocks<that.shocks; } Stat operator + (const Stat& that) const { return Stat(changes+that.changes, shocks+that.shocks); } }; int main() { const int STEPS = 500; { int n, m; cin >> n >> m; } string s1, s2; cin >> s1 >> s2; int di = s1.size()/STEPS +1; int dj = s2.size()/NumberOfNodes() +1; int j0 = MyNodeId()*dj; if(j0>s2.size()) return 0; Stat top[di]; Stat left[dj+1], actual[dj+1]; for(int i0=0; i0<=s1.size(); i0+=di) { if(j0>0) { Receive(MyNodeId()-1); //fprintf(stderr, "%d -> I#%d\n", i0/di, MyNodeId()); for(int i=i0; i<i0+di && i<=s1.size(); i++) { int c = GetInt(MyNodeId()-1); int s = GetInt(MyNodeId()-1); top[i-i0] = Stat(c, s); } } for(int i=i0; i<i0+di && i<=s1.size(); i++) { actual[0] = top[i-i0]; for(int j=j0; j<j0+dj && j<=s2.size(); j++) { if(i==0 || j==0) actual[j-j0 +1]=Stat(max(i, j), 0); else actual[j-j0 +1] = min3( actual[(j-1)-j0 +1] + Stat(1, 0), left[j-j0 +1] +Stat(1, 0), left[(j-1)-j0 +1] + ( s1[i-1]==s2[j-1] ? Stat(0,0) : Stat(1, s1[i-1]<s2[j-1] ? 1:0) ) ); } if(MyNodeId()+1 < NumberOfNodes()) { PutInt(MyNodeId()+1, actual[dj].changes); PutInt(MyNodeId()+1, actual[dj].shocks); } for(int j=j0; j<j0+dj && j<=s2.size(); j++) { left[j-j0 +1] = actual[j-j0 +1]; } left[0] = actual[0]; } if(MyNodeId()+1 < NumberOfNodes()) { Send(MyNodeId()+1); //fprintf(stderr, "I#%d ->%d\n", MyNodeId(), i0/di); } } if(j0+dj > s2.size()) printf("%d %d\n", actual[s2.size()-j0 +1].changes, actual[s2.size()-j0 +1].shocks); return 0; } |