#include "message.h" #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> // #include <unordered_set> // #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v,n) typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define mp(x,y) make_pair(x,y) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); struct Val { int d, z; Val() {} Val(int _d, int _z): d(_d), z(_z) { } bool operator < (const Val& a) const { if (d != a.d) return d < a.d; return z < a.z; } }; const int N = 100010; const int STEP = 200; const int MAXK = 10000; const int GR = 20; char a[N], b[N]; Val F[MAXK + 1][STEP + 1]; Val G[GR + 1][N]; int n, m; bool revers; bool dbg = false; int compute(int aPocz, int aKon, int bPocz, int bKon) { int row = 0; if (dbg) { printf("computing sub: [%d;%d] x [%d;%d]\n", aPocz, aKon-1, bPocz, bKon-1); FOR(i, bPocz - 1, bKon - 1) { printf("(%d, %d) ", F[0][i - bPocz + 1].d, F[0][i - bPocz + 1].z); } printf("\n"); } FOR(i, aPocz, aKon - 1) { ++row; int currJ = 0; if (dbg) printf("(%d, %d) ", F[row][currJ].d, F[row][currJ].z); FOR(j, bPocz, bKon - 1) { ++currJ; F[row][currJ] = min(F[row][currJ-1], F[row-1][currJ]); ++F[row][currJ].d; Val kand = F[row-1][currJ - 1]; if (a[i] != b[j]) { ++kand.d; if (!revers && a[i] < b[j]) { ++kand.z; } if (revers && a[i] > b[j]) { ++kand.z; } } if (kand < F[row][currJ]) { F[row][currJ] = kand; } if (dbg)printf("(%d, %d) ", F[row][currJ].d, F[row][currJ].z); } if (dbg)printf("\n"); } return row; } int computeBrut(int aPocz, int aKon, int bPocz, int bKon) { int row = 0; //printf("computing sub: [%d;%d] x [%d;%d]\n", aPocz, aKon-1, bPocz, bKon-1); //FOR(i, bPocz - 1, bKon - 1) { // printf("(%d, %d)=(%d, %d) ", 0, i - bPocz + 1, F[0][i - bPocz + 1].d, F[0][i - bPocz + 1].z); //} //printf("\n"); FOR(i, aPocz, aKon - 1) { ++row; int currJ = 0; //printf("(%d, %d)=(%d, %d) ", row, currJ, F[row][currJ].d, F[row][currJ].z); FOR(j, bPocz, bKon - 1) { ++currJ; G[row][currJ] = min(G[row][currJ-1], G[row-1][currJ]); ++G[row][currJ].d; Val kand = G[row-1][currJ - 1]; if (a[i] != b[j]) { ++kand.d; if (!revers && a[i] < b[j]) { ++kand.z; } if (revers && a[i] > b[j]) { ++kand.z; } } if (kand < G[row][currJ]) { G[row][currJ] = kand; } //printf("(%d, %d)=(%d, %d) ", row, currJ, F[row][currJ].d, F[row][currJ].z); } //printf("\n"); } return row; } int main() { scanf("%d %d", &n, &m); scanf("%s", a + 1); scanf("%s", b + 1); if (n > m) { revers = true; swap(n, m); REP(i, max(n, m)) { swap(a[i + 1], b[i + 1]); } } int I = NumberOfNodes(); int ID = MyNodeId(); if (n <= GR) { if (ID != 0) { return 0; } REP(i, m+1) G[0][i] = Val(i, 0); REP(i, n+1) G[i][0] = Val(i, 0); int wiersz = computeBrut(1, n + 1, 1, m + 1); printf("%d %d\n", G[n][m].d, G[n][m].z); return 0; } if (n < I) { I = n; if (ID >= I) { return 0; } } int pocz = 1 + (ID * n) / I; int kon = 1 + ((ID + 1) * n) / I; for (int x = pocz - 1; x <= kon; ++x) { F[x - pocz + 1][0] = Val(x, 0); } int od = 1, azDo; for (od = 1; od <= m; od += STEP) { azDo = min(od + STEP, m + 1); if (ID > 0) { int x = Receive(ID - 1); FOR(i, od, azDo-1) { F[0][i - od + 1].d = GetInt(x); F[0][i - od + 1].z = GetInt(x); } } else { FOR(i, od, azDo-1) { F[0][i - od + 1] = Val(i, 0); } } int wiersz = compute(pocz, kon, od, azDo); if (ID + 1 < I) { FOR(i, od, azDo-1) { PutInt(ID + 1, F[wiersz][i - od + 1].d); PutInt(ID + 1, F[wiersz][i - od + 1].z); } Send(ID + 1); } int lastColumn = azDo - od; FOR(x, pocz - 1, kon - 1) { F[x - pocz + 1][0] = F[x - pocz + 1][lastColumn]; //printf("przepisuje F[%d][%d] = F[%d][%d]\n", x - pocz + 1, 0, x - pocz + 1, lastColumn); } } if (ID == I - 1) { //printf("row: %d\n", kon - pocz); printf("%d %d\n", F[kon - pocz][0].d, F[kon - pocz][0].z); } 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | #include "message.h" #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <stack> #include <bitset> // #include <unordered_set> // #include <unordered_map> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; typedef short int sint; #define FOR(x, b, e) for(int x=(b); x<=(e); ++x) #define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v,n) typeof(n) v=(n) #define ALL(c) c.begin(),c.end() #define SIZE(x) ((int)((x).size())) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define mp(x,y) make_pair(x,y) #define DEBUG 1 #define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; } #define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }} #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); struct Val { int d, z; Val() {} Val(int _d, int _z): d(_d), z(_z) { } bool operator < (const Val& a) const { if (d != a.d) return d < a.d; return z < a.z; } }; const int N = 100010; const int STEP = 200; const int MAXK = 10000; const int GR = 20; char a[N], b[N]; Val F[MAXK + 1][STEP + 1]; Val G[GR + 1][N]; int n, m; bool revers; bool dbg = false; int compute(int aPocz, int aKon, int bPocz, int bKon) { int row = 0; if (dbg) { printf("computing sub: [%d;%d] x [%d;%d]\n", aPocz, aKon-1, bPocz, bKon-1); FOR(i, bPocz - 1, bKon - 1) { printf("(%d, %d) ", F[0][i - bPocz + 1].d, F[0][i - bPocz + 1].z); } printf("\n"); } FOR(i, aPocz, aKon - 1) { ++row; int currJ = 0; if (dbg) printf("(%d, %d) ", F[row][currJ].d, F[row][currJ].z); FOR(j, bPocz, bKon - 1) { ++currJ; F[row][currJ] = min(F[row][currJ-1], F[row-1][currJ]); ++F[row][currJ].d; Val kand = F[row-1][currJ - 1]; if (a[i] != b[j]) { ++kand.d; if (!revers && a[i] < b[j]) { ++kand.z; } if (revers && a[i] > b[j]) { ++kand.z; } } if (kand < F[row][currJ]) { F[row][currJ] = kand; } if (dbg)printf("(%d, %d) ", F[row][currJ].d, F[row][currJ].z); } if (dbg)printf("\n"); } return row; } int computeBrut(int aPocz, int aKon, int bPocz, int bKon) { int row = 0; //printf("computing sub: [%d;%d] x [%d;%d]\n", aPocz, aKon-1, bPocz, bKon-1); //FOR(i, bPocz - 1, bKon - 1) { // printf("(%d, %d)=(%d, %d) ", 0, i - bPocz + 1, F[0][i - bPocz + 1].d, F[0][i - bPocz + 1].z); //} //printf("\n"); FOR(i, aPocz, aKon - 1) { ++row; int currJ = 0; //printf("(%d, %d)=(%d, %d) ", row, currJ, F[row][currJ].d, F[row][currJ].z); FOR(j, bPocz, bKon - 1) { ++currJ; G[row][currJ] = min(G[row][currJ-1], G[row-1][currJ]); ++G[row][currJ].d; Val kand = G[row-1][currJ - 1]; if (a[i] != b[j]) { ++kand.d; if (!revers && a[i] < b[j]) { ++kand.z; } if (revers && a[i] > b[j]) { ++kand.z; } } if (kand < G[row][currJ]) { G[row][currJ] = kand; } //printf("(%d, %d)=(%d, %d) ", row, currJ, F[row][currJ].d, F[row][currJ].z); } //printf("\n"); } return row; } int main() { scanf("%d %d", &n, &m); scanf("%s", a + 1); scanf("%s", b + 1); if (n > m) { revers = true; swap(n, m); REP(i, max(n, m)) { swap(a[i + 1], b[i + 1]); } } int I = NumberOfNodes(); int ID = MyNodeId(); if (n <= GR) { if (ID != 0) { return 0; } REP(i, m+1) G[0][i] = Val(i, 0); REP(i, n+1) G[i][0] = Val(i, 0); int wiersz = computeBrut(1, n + 1, 1, m + 1); printf("%d %d\n", G[n][m].d, G[n][m].z); return 0; } if (n < I) { I = n; if (ID >= I) { return 0; } } int pocz = 1 + (ID * n) / I; int kon = 1 + ((ID + 1) * n) / I; for (int x = pocz - 1; x <= kon; ++x) { F[x - pocz + 1][0] = Val(x, 0); } int od = 1, azDo; for (od = 1; od <= m; od += STEP) { azDo = min(od + STEP, m + 1); if (ID > 0) { int x = Receive(ID - 1); FOR(i, od, azDo-1) { F[0][i - od + 1].d = GetInt(x); F[0][i - od + 1].z = GetInt(x); } } else { FOR(i, od, azDo-1) { F[0][i - od + 1] = Val(i, 0); } } int wiersz = compute(pocz, kon, od, azDo); if (ID + 1 < I) { FOR(i, od, azDo-1) { PutInt(ID + 1, F[wiersz][i - od + 1].d); PutInt(ID + 1, F[wiersz][i - od + 1].z); } Send(ID + 1); } int lastColumn = azDo - od; FOR(x, pocz - 1, kon - 1) { F[x - pocz + 1][0] = F[x - pocz + 1][lastColumn]; //printf("przepisuje F[%d][%d] = F[%d][%d]\n", x - pocz + 1, 0, x - pocz + 1, lastColumn); } } if (ID == I - 1) { //printf("row: %d\n", kon - pocz); printf("%d %d\n", F[kon - pocz][0].d, F[kon - pocz][0].z); } return 0; } |