#include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); #define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } inline pii licz (pii skos, pii top, pii le, char aa, char bb) { if (aa == bb) { return skos; } else { pii costDel(le.fi + 1, le.se); pii costIns(top.fi + 1, top.se); pii costRepl(skos.fi + 1, skos.se + (aa < bb)); return min(min(costDel, costIns), costRepl); } } #include "message.h" #define N 100007 char a[N], b[N]; pii Left[N], Top[N], Right[N], Bottom[N]; pii dp[2][N]; //const int D = 1; int main () { wez2(_,__); scanf("%s %s", a, b); if (strlen(a) < strlen(b)) { // swap const int leng = max(strlen(a), strlen(b)); REP(i,0,leng+1) swap(a[i],b[i]); } const int n = strlen(a), m = strlen(b); //DBG(a) //DBG(b) // współrzędne 1-based REPD(i,n,1) a[i] = a[i-1]; REPD(i,m,1) b[i] = b[i-1]; // n >= m const int bm = min(NumberOfNodes(), m); const int my = MyNodeId() + 1; // workerzy tez 1-based: [1..bm] if (my > bm) { // jestem niepotrzebny :C return 0; } // test /*int nowDebug = 1; //if (my > 1) return 0; pii DP[500][500]; REP(i,0,max(n,m)) DP[i][0] = DP[0][i] = mp(i,0); REP(i,1,m) REP(j,1,n) { DP[j][i] = licz(DP[j-1][i-1], DP[j][i-1], DP[j-1][i], a[j], b[i]); if (my == nowDebug) { if (j == 1 && i == 2) { DBG(DP[1][2]) DBG(DP[j-1][i-1]) DBG(DP[j][i-1]) DBG(DP[j-1][i]) DBG(a[j]) DBG(b[i]) } } }*/ /*if (my == nowDebug) { DBG(DP[1][3]) REP(i,0,m) { REP(j,0,n) cerr << DP[j][i] << " "; cerr << endl; } }*/ //DBG(DP[n][m]) //return 0; // test const int bn = (1LL * bm * n) / m; // jestem workerem my // budze sie w rundzie my int kn = my, km = 0; REP(runda,my,bm+bn-my) { // update km, kn if (runda <= bm) { ++km; } else { ++kn; } // dla bloku (km,kn) const int fromM = (1LL * m * (km-1)) / bm + 1; const int toM = (1LL * m * km) / bm; const int fromN = (1LL * n * (kn-1)) / bn + 1; const int toN = (1LL * n * kn) / bn; /*if (my == nowDebug) { DBG(runda)DBG(km)DBG(kn) DBG(fromM)DBG(toM)DBG(fromN)DBG(toN) cerr<<endl; }*/ // pobieram Left if (fromN == 1) { // przypadek bazowy w DP REP(i,fromM-1,toM) Left[i] = mp(i,0); } else if (runda <= bm) { // odbieram od typa z lewej /*if (my == nowDebug) { DBG("odbieram z lewej od") DBG(my-1) }*/ int od = my - 1 - 1; Receive(od); REP(i,fromM-1,toM) { const int int1 = GetInt(od); const int int2 = GetInt(od); Left[i] = mp(int1,int2); } } else { // odbieram od samego siebie // powinno juz byc pobrane } // pobieram Top if (fromM == 1) { // przypadek bazowy w DP REP(j,fromN-1,toN) Top[j] = mp(j,0); } else if (runda > bm) { // odbieram od typa z gory int odKogo = my + 1 - 1; Receive(odKogo); REP(j,fromN-1,toN) { const int int1 = GetInt(odKogo); const int int2 = GetInt(odKogo); Top[j] = mp(int1,int2); } } else { // odbieram od samego siebie // powinno byc juz pobrane } //if (D) assert(Left[fromM-1] == Top[fromN-1]); // debug /*if (my == nowDebug) { REP(j,fromN-1,toN) if (!(DP[j][fromM-1] == Top[j])) { DBG(j) DBG(fromM-1) DBG(DP[j][fromM-1]) DBG(Top[j]) return 1; } REP(i,fromM-1,toM) if (!(DP[fromN-1][i] == Left[i])) { DBG(fromN-1) DBG(i) DBG(DP[fromN-1][i]) DBG(Left[i]) return 1; } }*/ // wyliczam Right[fromM-1,toM] i Bottom[fromN-1,toN] Bottom[fromN-1] = Left[toM]; Right[fromM-1] = Top[toN]; // wyliczam Right[fromM,toM] i Bottom[fromN,toN] dp[fromN & 1][fromM] = licz(Top[fromN-1], Top[fromN], Left[fromM], a[fromN], b[fromM]); REP(i,fromM+1,toM) { dp[fromN & 1][i] = licz(Left[i-1], dp[fromN & 1][i-1], Left[i], a[fromN], b[i]); } Bottom[fromN] = dp[fromN & 1][toM]; REP(j,fromN+1,toN) { dp[j & 1][fromM] = licz(Top[j-1], Top[j], dp[(j-1) & 1][fromM], a[j], b[fromM]); REP(i,fromM+1,toM) { dp[j & 1][i] = licz(dp[(j-1) & 1][i-1], dp[j & 1][i-1], dp[(j-1) & 1][i], a[j], b[i]); } Bottom[j] = dp[j & 1][toM]; } /*REP(j,fromN,toN) { REP(i,fromM,toM) { dp[j & 1][i] = licz(j == fromN ? Left[i-1] : i == fromM ? Top[j-1] : dp[(j-1) & 1][i], i == fromM ? Top[j] : dp[j&1][i-1], j == fromN ? Left[i] : dp[(j-1) & 1][i], a[j], b[i]); } Bottom[j] = dp[j & 1][toM]; }*/ REP(i,fromM,toM) { Right[i] = dp[toN & 1][i]; } if (runda < bm) { // w prawo do my+1 int doKogo = my + 1 - 1; REP(i,fromM-1,toM) { PutInt(doKogo, Right[i].fi); PutInt(doKogo, Right[i].se); } Send(doKogo); // w dol do siebie REP(j,fromN-1,toN) { Top[j] = Bottom[j]; } } else { // runda >= bm // w dol do my-1 if (my != 1) { int doKogo = my - 1 - 1; REP(j,fromN-1,toN) { PutInt(doKogo, Bottom[j].fi); PutInt(doKogo, Bottom[j].se); } Send(doKogo); } // w prawo do siebie REP(i,fromM-1,toM) { Left[i] = Right[i]; } } if (km == bm && kn == bn) { //if (D) assert(my == 1 && runda == bm+bn-my && Right[m] == Bottom[n]); printf("%d %d\n", Right[m].fi, Right[m].se); } } 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | #include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); #define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } inline pii licz (pii skos, pii top, pii le, char aa, char bb) { if (aa == bb) { return skos; } else { pii costDel(le.fi + 1, le.se); pii costIns(top.fi + 1, top.se); pii costRepl(skos.fi + 1, skos.se + (aa < bb)); return min(min(costDel, costIns), costRepl); } } #include "message.h" #define N 100007 char a[N], b[N]; pii Left[N], Top[N], Right[N], Bottom[N]; pii dp[2][N]; //const int D = 1; int main () { wez2(_,__); scanf("%s %s", a, b); if (strlen(a) < strlen(b)) { // swap const int leng = max(strlen(a), strlen(b)); REP(i,0,leng+1) swap(a[i],b[i]); } const int n = strlen(a), m = strlen(b); //DBG(a) //DBG(b) // współrzędne 1-based REPD(i,n,1) a[i] = a[i-1]; REPD(i,m,1) b[i] = b[i-1]; // n >= m const int bm = min(NumberOfNodes(), m); const int my = MyNodeId() + 1; // workerzy tez 1-based: [1..bm] if (my > bm) { // jestem niepotrzebny :C return 0; } // test /*int nowDebug = 1; //if (my > 1) return 0; pii DP[500][500]; REP(i,0,max(n,m)) DP[i][0] = DP[0][i] = mp(i,0); REP(i,1,m) REP(j,1,n) { DP[j][i] = licz(DP[j-1][i-1], DP[j][i-1], DP[j-1][i], a[j], b[i]); if (my == nowDebug) { if (j == 1 && i == 2) { DBG(DP[1][2]) DBG(DP[j-1][i-1]) DBG(DP[j][i-1]) DBG(DP[j-1][i]) DBG(a[j]) DBG(b[i]) } } }*/ /*if (my == nowDebug) { DBG(DP[1][3]) REP(i,0,m) { REP(j,0,n) cerr << DP[j][i] << " "; cerr << endl; } }*/ //DBG(DP[n][m]) //return 0; // test const int bn = (1LL * bm * n) / m; // jestem workerem my // budze sie w rundzie my int kn = my, km = 0; REP(runda,my,bm+bn-my) { // update km, kn if (runda <= bm) { ++km; } else { ++kn; } // dla bloku (km,kn) const int fromM = (1LL * m * (km-1)) / bm + 1; const int toM = (1LL * m * km) / bm; const int fromN = (1LL * n * (kn-1)) / bn + 1; const int toN = (1LL * n * kn) / bn; /*if (my == nowDebug) { DBG(runda)DBG(km)DBG(kn) DBG(fromM)DBG(toM)DBG(fromN)DBG(toN) cerr<<endl; }*/ // pobieram Left if (fromN == 1) { // przypadek bazowy w DP REP(i,fromM-1,toM) Left[i] = mp(i,0); } else if (runda <= bm) { // odbieram od typa z lewej /*if (my == nowDebug) { DBG("odbieram z lewej od") DBG(my-1) }*/ int od = my - 1 - 1; Receive(od); REP(i,fromM-1,toM) { const int int1 = GetInt(od); const int int2 = GetInt(od); Left[i] = mp(int1,int2); } } else { // odbieram od samego siebie // powinno juz byc pobrane } // pobieram Top if (fromM == 1) { // przypadek bazowy w DP REP(j,fromN-1,toN) Top[j] = mp(j,0); } else if (runda > bm) { // odbieram od typa z gory int odKogo = my + 1 - 1; Receive(odKogo); REP(j,fromN-1,toN) { const int int1 = GetInt(odKogo); const int int2 = GetInt(odKogo); Top[j] = mp(int1,int2); } } else { // odbieram od samego siebie // powinno byc juz pobrane } //if (D) assert(Left[fromM-1] == Top[fromN-1]); // debug /*if (my == nowDebug) { REP(j,fromN-1,toN) if (!(DP[j][fromM-1] == Top[j])) { DBG(j) DBG(fromM-1) DBG(DP[j][fromM-1]) DBG(Top[j]) return 1; } REP(i,fromM-1,toM) if (!(DP[fromN-1][i] == Left[i])) { DBG(fromN-1) DBG(i) DBG(DP[fromN-1][i]) DBG(Left[i]) return 1; } }*/ // wyliczam Right[fromM-1,toM] i Bottom[fromN-1,toN] Bottom[fromN-1] = Left[toM]; Right[fromM-1] = Top[toN]; // wyliczam Right[fromM,toM] i Bottom[fromN,toN] dp[fromN & 1][fromM] = licz(Top[fromN-1], Top[fromN], Left[fromM], a[fromN], b[fromM]); REP(i,fromM+1,toM) { dp[fromN & 1][i] = licz(Left[i-1], dp[fromN & 1][i-1], Left[i], a[fromN], b[i]); } Bottom[fromN] = dp[fromN & 1][toM]; REP(j,fromN+1,toN) { dp[j & 1][fromM] = licz(Top[j-1], Top[j], dp[(j-1) & 1][fromM], a[j], b[fromM]); REP(i,fromM+1,toM) { dp[j & 1][i] = licz(dp[(j-1) & 1][i-1], dp[j & 1][i-1], dp[(j-1) & 1][i], a[j], b[i]); } Bottom[j] = dp[j & 1][toM]; } /*REP(j,fromN,toN) { REP(i,fromM,toM) { dp[j & 1][i] = licz(j == fromN ? Left[i-1] : i == fromM ? Top[j-1] : dp[(j-1) & 1][i], i == fromM ? Top[j] : dp[j&1][i-1], j == fromN ? Left[i] : dp[(j-1) & 1][i], a[j], b[i]); } Bottom[j] = dp[j & 1][toM]; }*/ REP(i,fromM,toM) { Right[i] = dp[toN & 1][i]; } if (runda < bm) { // w prawo do my+1 int doKogo = my + 1 - 1; REP(i,fromM-1,toM) { PutInt(doKogo, Right[i].fi); PutInt(doKogo, Right[i].se); } Send(doKogo); // w dol do siebie REP(j,fromN-1,toN) { Top[j] = Bottom[j]; } } else { // runda >= bm // w dol do my-1 if (my != 1) { int doKogo = my - 1 - 1; REP(j,fromN-1,toN) { PutInt(doKogo, Bottom[j].fi); PutInt(doKogo, Bottom[j].se); } Send(doKogo); } // w prawo do siebie REP(i,fromM-1,toM) { Left[i] = Right[i]; } } if (km == bm && kn == bn) { //if (D) assert(my == 1 && runda == bm+bn-my && Right[m] == Bottom[n]); printf("%d %d\n", Right[m].fi, Right[m].se); } } return 0; } |