#include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <list> #include <iostream> #include <cstdarg> #include "message.h" using namespace std; #define FOR(i,n) for(int i = 0; i < n; i++) #define REP(i,n) for(int i = 1; i <= n; i++) #define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) #define frs first #define sec second #define psh push_back #define mkp make_pair typedef long long LL; typedef long double LD; const int INF = 2147483647; const int MAX = 1010; const int MAX_INPUT = 100100; int N, NR; #define FINISH 0x0 #define WHO 0x1 #define NEIGH 0x2 #define ORIENT 0x3 #define POS 0x4 int AN[] = {0, 2, 2, 1, 1}; void send(int nr, int cmd, ...) { va_list args; va_start(args, cmd); PutInt(nr, cmd); for(int i = 0; i < AN[cmd]; i++) { PutInt(nr, va_arg(args, int)); } va_end(args); Send(nr); } int receive(int nr, int *cmd, ...) { int ret; va_list args; va_start(args, cmd); ret = Receive(nr); *cmd = GetInt(ret); for(int i = 0; i < AN[*cmd]; i++) { *(va_arg(args, int*)) = GetInt(ret); } va_end(args); return ret; } int n,m; char str1[MAX_INPUT], str2[MAX_INPUT]; vector<vector<pair<int,int>>> DP; int STEP, OFF_BEG, OFF_END; pair<int,int> min(const pair<int,int> &a, const pair<int,int> &b) { if(a.frs == b.frs) { if(a.sec < b.sec) return a; else return b; } if(a.frs < b.frs) return a; else return b; } void getUp(int off) { if(off == 0) { for(int i = 0; i <= STEP; i++) DP[0][i] = mkp(OFF_BEG + i, 0); } else { DP[0].swap(DP[STEP]); } } void getLeft(int off) { if(NR == 0) { for(int i = 0; i <= STEP; i++) DP[i][0] = mkp(off + i, 0); } else { Receive(NR - 1); for(int i = 1; i <= STEP; i++) { DP[i][0].frs = GetInt(NR - 1); DP[i][0].sec = GetInt(NR - 1); } } } void sendRight() { if(NR == N - 1) return; for(int i = 1; i <= STEP; i++) { PutInt(NR + 1, DP[i][STEP].frs); PutInt(NR + 1, DP[i][STEP].sec); } Send(NR + 1); } pair<int,int> operator+(pair<int,int> a, pair<int,int> b) { a.frs += b.frs; a.sec += b.sec; return a; } void fill(int off) { int mm = OFF_END - OFF_BEG + 1; int nn = min(STEP, n - off); for(int j = 1; j <= mm; j++) { for(int i = 1; i <= nn; i++) { if(str1[i + off - 1] == str2[j + OFF_BEG - 1]) DP[i][j] = DP[i-1][j-1]; else if(str1[i + off - 1] < str2[j + OFF_BEG - 1]) DP[i][j] = DP[i - 1][j - 1] + mkp(1,1); else DP[i][j] = DP[i - 1][j - 1] + mkp(1, 0); DP[i][j] = min(DP[i][j], DP[i - 1][j] + mkp(1,0)); DP[i][j] = min(DP[i][j], DP[i][j - 1] + mkp(1,0)); // fprintf(stderr, "DP[%d][%d] = (%d,%d)\n", i + off, j + OFF_BEG, DP[i][j].frs, // DP[i][j].sec); } } } int main() { scanf("%d%d", &n, &m); scanf("%s %s", str1, str2); N = min(NumberOfNodes(), m); NR = MyNodeId(); if(NR > N - 1) return 0; STEP = (m + N -1)/ N; OFF_BEG = STEP * NR; OFF_END = min(STEP * (NR + 1) - 1, m - 1); fprintf(stderr, "NR(%d) : (%d,%d)\n", NR, OFF_BEG, OFF_END); DP.resize(STEP + 1, vector<pair<int,int>>(STEP + 1, mkp(0,0))); for(int i = 0; i * STEP <= n; i++) { getUp(i * STEP); getLeft(i * STEP); fill(i * STEP); sendRight(); if((i + 1) * STEP > n && NR == N -1) { pair<int,int> ans = DP[n - (i*STEP)][m - OFF_BEG]; printf("%d %d\n", ans.frs, ans.sec); } } }
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 | #include <string> #include <vector> #include <map> #include <cmath> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <list> #include <iostream> #include <cstdarg> #include "message.h" using namespace std; #define FOR(i,n) for(int i = 0; i < n; i++) #define REP(i,n) for(int i = 1; i <= n; i++) #define FORI(it,n)for(typeof(n.begin()) it = n.begin(); it != n.end(); it++) #define frs first #define sec second #define psh push_back #define mkp make_pair typedef long long LL; typedef long double LD; const int INF = 2147483647; const int MAX = 1010; const int MAX_INPUT = 100100; int N, NR; #define FINISH 0x0 #define WHO 0x1 #define NEIGH 0x2 #define ORIENT 0x3 #define POS 0x4 int AN[] = {0, 2, 2, 1, 1}; void send(int nr, int cmd, ...) { va_list args; va_start(args, cmd); PutInt(nr, cmd); for(int i = 0; i < AN[cmd]; i++) { PutInt(nr, va_arg(args, int)); } va_end(args); Send(nr); } int receive(int nr, int *cmd, ...) { int ret; va_list args; va_start(args, cmd); ret = Receive(nr); *cmd = GetInt(ret); for(int i = 0; i < AN[*cmd]; i++) { *(va_arg(args, int*)) = GetInt(ret); } va_end(args); return ret; } int n,m; char str1[MAX_INPUT], str2[MAX_INPUT]; vector<vector<pair<int,int>>> DP; int STEP, OFF_BEG, OFF_END; pair<int,int> min(const pair<int,int> &a, const pair<int,int> &b) { if(a.frs == b.frs) { if(a.sec < b.sec) return a; else return b; } if(a.frs < b.frs) return a; else return b; } void getUp(int off) { if(off == 0) { for(int i = 0; i <= STEP; i++) DP[0][i] = mkp(OFF_BEG + i, 0); } else { DP[0].swap(DP[STEP]); } } void getLeft(int off) { if(NR == 0) { for(int i = 0; i <= STEP; i++) DP[i][0] = mkp(off + i, 0); } else { Receive(NR - 1); for(int i = 1; i <= STEP; i++) { DP[i][0].frs = GetInt(NR - 1); DP[i][0].sec = GetInt(NR - 1); } } } void sendRight() { if(NR == N - 1) return; for(int i = 1; i <= STEP; i++) { PutInt(NR + 1, DP[i][STEP].frs); PutInt(NR + 1, DP[i][STEP].sec); } Send(NR + 1); } pair<int,int> operator+(pair<int,int> a, pair<int,int> b) { a.frs += b.frs; a.sec += b.sec; return a; } void fill(int off) { int mm = OFF_END - OFF_BEG + 1; int nn = min(STEP, n - off); for(int j = 1; j <= mm; j++) { for(int i = 1; i <= nn; i++) { if(str1[i + off - 1] == str2[j + OFF_BEG - 1]) DP[i][j] = DP[i-1][j-1]; else if(str1[i + off - 1] < str2[j + OFF_BEG - 1]) DP[i][j] = DP[i - 1][j - 1] + mkp(1,1); else DP[i][j] = DP[i - 1][j - 1] + mkp(1, 0); DP[i][j] = min(DP[i][j], DP[i - 1][j] + mkp(1,0)); DP[i][j] = min(DP[i][j], DP[i][j - 1] + mkp(1,0)); // fprintf(stderr, "DP[%d][%d] = (%d,%d)\n", i + off, j + OFF_BEG, DP[i][j].frs, // DP[i][j].sec); } } } int main() { scanf("%d%d", &n, &m); scanf("%s %s", str1, str2); N = min(NumberOfNodes(), m); NR = MyNodeId(); if(NR > N - 1) return 0; STEP = (m + N -1)/ N; OFF_BEG = STEP * NR; OFF_END = min(STEP * (NR + 1) - 1, m - 1); fprintf(stderr, "NR(%d) : (%d,%d)\n", NR, OFF_BEG, OFF_END); DP.resize(STEP + 1, vector<pair<int,int>>(STEP + 1, mkp(0,0))); for(int i = 0; i * STEP <= n; i++) { getUp(i * STEP); getLeft(i * STEP); fill(i * STEP); sendRight(); if((i + 1) * STEP > n && NR == N -1) { pair<int,int> ans = DP[n - (i*STEP)][m - OFF_BEG]; printf("%d %d\n", ans.frs, ans.sec); } } } |