//Przemysław Jakub Kozłowski #include "message.h" #include <iostream> #include <cstdio> #include <algorithm> #define FI first #define SE second #define MP make_pair using namespace std; typedef long long LL; const int N = 100005, M = 100005, W = 2007, BRUT = 10000007; const int MAXW = W; void brut(); pair<int,int> GetPI(int cel); void PutPI(int cel, pair<int,int> a); void obl(char *A, char *B, pair<int,int> tab[MAXW+7][MAXW+7]); int n,m; char A[N], B[M]; pair<int,int> tab[MAXW+7][MAXW+7]; int main() { scanf("%d%d", &n, &m); scanf(" %s", A+1); scanf(" %s", B+1); if((LL)n*m <= BRUT) { if(MyNodeId() != 0) return 0; brut(); return 0; } int nW = n/W+(n%W == 0 ? 0 : 1); int mW = m/W+(m%W == 0 ? 0 : 1); if(MyNodeId()+1 > min(nW, mW)) return 0; // 1 Etap for(int i = 1;i <= W;i++) tab[W][i] = MP(i,0); for(int i = 1;i <= nW-MyNodeId()-1;i++) { //cerr << "LIA: " << i << endl; // Lewa strona if(MyNodeId() > 0) { //cerr << "Czekam" << endl; Receive(MyNodeId()-1); //cerr << "Odebralem" << endl; for(int j = 0;j <= W;j++) tab[j][0] = GetPI(MyNodeId()-1); } else { for(int j = 0;j <= W;j++) tab[j][0] = MP((i-1)*W+j, 0); } //cerr << "LIB: " << i << endl; // Góra for(int j = 1;j <= W;j++) tab[0][j] = tab[W][j]; //cerr << "LIC: " << i << endl; obl(A+(i-1)*W, B+MyNodeId()*W, tab); //cerr << "LID: " << i << endl; if(MyNodeId() < mW) { //cerr << "Wysylam" << endl; for(int j = 0;j <= W;j++) PutPI(MyNodeId()+1, tab[j][W]); Send(MyNodeId()+1); } //cerr << "LIE: " << i << endl; } //cerr << "Srodkowy Etap" << endl; // Środkowy etap // Góra for(int j = 1;j <= W;j++) tab[0][j] = tab[W][j]; // Lewa strona if(MyNodeId() > 0) { Receive(MyNodeId()-1); for(int j = 0;j <= W;j++) tab[j][0] = GetPI(MyNodeId()-1); } else { for(int j = 0;j <= W;j++) tab[j][0] = MP((nW-1)*W+j, 0); } obl(A+(nW-MyNodeId()-1)*W, B+MyNodeId()*W, tab); /*cerr << "TAB: " << endl; for(int i = 0;i <= n;i++) { for(int j = 0;j <= m;j++) cerr << "(" << tab[i][j].FI << "," << tab[i][j].SE << ")" << " "; cerr << endl; }*/ if(MyNodeId() > 0) { for(int j = 0;j <= W;j++) PutPI(MyNodeId()-1, tab[W][j]); Send(MyNodeId()-1); } //cerr << "2 Etap" << endl; // 2 Etap for(int i = 1+MyNodeId()+1;i <= mW;i++) { //cerr << "PI: " << i << endl; // Góra Receive(MyNodeId()+1); for(int j = 0;j <= W;j++) tab[0][j] = GetPI(MyNodeId()+1); // Lewa strona for(int j = 1;j <= W;j++) tab[j][0] = tab[j][W]; obl(A+(nW-MyNodeId()-1)*W, B+(i-1)*W, tab); if(MyNodeId() > 0) { for(int j = 0;j <= W;j++) PutPI(MyNodeId()-1, tab[W][j]); Send(MyNodeId()-1); } } //cerr << "Koniec" << endl; if(MyNodeId() == 0) { pair<int,int> wyn = tab[(n-1)%W+1][(m-1)%W+1]; printf("%d %d\n", wyn.FI, wyn.SE); } return 0; } void obl(char *A, char *B, pair<int,int> tab[MAXW+7][MAXW+7]) { for(int i = 1;i <= W;i++) for(int j = 1;j <= W;j++) { if(A[i] == B[j]) tab[i][j] = tab[i-1][j-1]; else { tab[i][j] = min(min(tab[i-1][j], tab[i][j-1]), MP(tab[i-1][j-1].FI, tab[i-1][j-1].SE+(A[i]<B[j]?1:0)) ); tab[i][j].FI++; } } } pair<int,int> GetPI(int cel) { pair<int,int> ret; ret.FI = GetInt(cel); ret.SE = GetInt(cel); return ret; } void PutPI(int cel, pair<int,int> a) { PutInt(cel, a.FI); PutInt(cel, a.SE); } void brut() { //cerr << "Brut" << endl; pair<int,int> *tab = new pair<int,int>[(n+2)*(m+2)+7]; for(int i = 1;i <= n;i++) tab[i*(m+1)+0] = MP(i,0); for(int i = 1;i <= m;i++) tab[0*(m+1)+i] = MP(i,0); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) { if(A[i] == B[j]) tab[i*(m+1)+j] = tab[(i-1)*(m+1)+j-1]; else { //cerr << "I: " << i << " J: " << j << " tab[i][j]: " << tab[i*(m+1)+j].FI << " A: " << tab[(i-1)*(m+1)+j].FI << " B: " << tab[i*(m+1)+j-1].FI << " C: " << MP(tab[(i-1)*(m+1)+j-1].FI, tab[(i-1)*(m+1)+j-1].SE+(A[i]<B[j]?1:0)).FI << endl; tab[i*(m+1)+j] = min(min(tab[(i-1)*(m+1)+j], tab[i*(m+1)+j-1]), MP(tab[(i-1)*(m+1)+j-1].FI, tab[(i-1)*(m+1)+j-1].SE+(A[i]<B[j]?1:0)) ); tab[i*(m+1)+j].FI++; } //cerr << "I: " << i << " J: " << j << " tab[i][j]: " << tab[i*(m+1)+j].FI << endl; } printf("%d %d\n", tab[n*(m+1)+m].FI, tab[n*(m+1)+m].SE); delete [] tab; }
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 204 205 | //Przemysław Jakub Kozłowski #include "message.h" #include <iostream> #include <cstdio> #include <algorithm> #define FI first #define SE second #define MP make_pair using namespace std; typedef long long LL; const int N = 100005, M = 100005, W = 2007, BRUT = 10000007; const int MAXW = W; void brut(); pair<int,int> GetPI(int cel); void PutPI(int cel, pair<int,int> a); void obl(char *A, char *B, pair<int,int> tab[MAXW+7][MAXW+7]); int n,m; char A[N], B[M]; pair<int,int> tab[MAXW+7][MAXW+7]; int main() { scanf("%d%d", &n, &m); scanf(" %s", A+1); scanf(" %s", B+1); if((LL)n*m <= BRUT) { if(MyNodeId() != 0) return 0; brut(); return 0; } int nW = n/W+(n%W == 0 ? 0 : 1); int mW = m/W+(m%W == 0 ? 0 : 1); if(MyNodeId()+1 > min(nW, mW)) return 0; // 1 Etap for(int i = 1;i <= W;i++) tab[W][i] = MP(i,0); for(int i = 1;i <= nW-MyNodeId()-1;i++) { //cerr << "LIA: " << i << endl; // Lewa strona if(MyNodeId() > 0) { //cerr << "Czekam" << endl; Receive(MyNodeId()-1); //cerr << "Odebralem" << endl; for(int j = 0;j <= W;j++) tab[j][0] = GetPI(MyNodeId()-1); } else { for(int j = 0;j <= W;j++) tab[j][0] = MP((i-1)*W+j, 0); } //cerr << "LIB: " << i << endl; // Góra for(int j = 1;j <= W;j++) tab[0][j] = tab[W][j]; //cerr << "LIC: " << i << endl; obl(A+(i-1)*W, B+MyNodeId()*W, tab); //cerr << "LID: " << i << endl; if(MyNodeId() < mW) { //cerr << "Wysylam" << endl; for(int j = 0;j <= W;j++) PutPI(MyNodeId()+1, tab[j][W]); Send(MyNodeId()+1); } //cerr << "LIE: " << i << endl; } //cerr << "Srodkowy Etap" << endl; // Środkowy etap // Góra for(int j = 1;j <= W;j++) tab[0][j] = tab[W][j]; // Lewa strona if(MyNodeId() > 0) { Receive(MyNodeId()-1); for(int j = 0;j <= W;j++) tab[j][0] = GetPI(MyNodeId()-1); } else { for(int j = 0;j <= W;j++) tab[j][0] = MP((nW-1)*W+j, 0); } obl(A+(nW-MyNodeId()-1)*W, B+MyNodeId()*W, tab); /*cerr << "TAB: " << endl; for(int i = 0;i <= n;i++) { for(int j = 0;j <= m;j++) cerr << "(" << tab[i][j].FI << "," << tab[i][j].SE << ")" << " "; cerr << endl; }*/ if(MyNodeId() > 0) { for(int j = 0;j <= W;j++) PutPI(MyNodeId()-1, tab[W][j]); Send(MyNodeId()-1); } //cerr << "2 Etap" << endl; // 2 Etap for(int i = 1+MyNodeId()+1;i <= mW;i++) { //cerr << "PI: " << i << endl; // Góra Receive(MyNodeId()+1); for(int j = 0;j <= W;j++) tab[0][j] = GetPI(MyNodeId()+1); // Lewa strona for(int j = 1;j <= W;j++) tab[j][0] = tab[j][W]; obl(A+(nW-MyNodeId()-1)*W, B+(i-1)*W, tab); if(MyNodeId() > 0) { for(int j = 0;j <= W;j++) PutPI(MyNodeId()-1, tab[W][j]); Send(MyNodeId()-1); } } //cerr << "Koniec" << endl; if(MyNodeId() == 0) { pair<int,int> wyn = tab[(n-1)%W+1][(m-1)%W+1]; printf("%d %d\n", wyn.FI, wyn.SE); } return 0; } void obl(char *A, char *B, pair<int,int> tab[MAXW+7][MAXW+7]) { for(int i = 1;i <= W;i++) for(int j = 1;j <= W;j++) { if(A[i] == B[j]) tab[i][j] = tab[i-1][j-1]; else { tab[i][j] = min(min(tab[i-1][j], tab[i][j-1]), MP(tab[i-1][j-1].FI, tab[i-1][j-1].SE+(A[i]<B[j]?1:0)) ); tab[i][j].FI++; } } } pair<int,int> GetPI(int cel) { pair<int,int> ret; ret.FI = GetInt(cel); ret.SE = GetInt(cel); return ret; } void PutPI(int cel, pair<int,int> a) { PutInt(cel, a.FI); PutInt(cel, a.SE); } void brut() { //cerr << "Brut" << endl; pair<int,int> *tab = new pair<int,int>[(n+2)*(m+2)+7]; for(int i = 1;i <= n;i++) tab[i*(m+1)+0] = MP(i,0); for(int i = 1;i <= m;i++) tab[0*(m+1)+i] = MP(i,0); for(int i = 1;i <= n;i++) for(int j = 1;j <= m;j++) { if(A[i] == B[j]) tab[i*(m+1)+j] = tab[(i-1)*(m+1)+j-1]; else { //cerr << "I: " << i << " J: " << j << " tab[i][j]: " << tab[i*(m+1)+j].FI << " A: " << tab[(i-1)*(m+1)+j].FI << " B: " << tab[i*(m+1)+j-1].FI << " C: " << MP(tab[(i-1)*(m+1)+j-1].FI, tab[(i-1)*(m+1)+j-1].SE+(A[i]<B[j]?1:0)).FI << endl; tab[i*(m+1)+j] = min(min(tab[(i-1)*(m+1)+j], tab[i*(m+1)+j-1]), MP(tab[(i-1)*(m+1)+j-1].FI, tab[(i-1)*(m+1)+j-1].SE+(A[i]<B[j]?1:0)) ); tab[i*(m+1)+j].FI++; } //cerr << "I: " << i << " J: " << j << " tab[i][j]: " << tab[i*(m+1)+j].FI << endl; } printf("%d %d\n", tab[n*(m+1)+m].FI, tab[n*(m+1)+m].SE); delete [] tab; } |