//Michal Wos MIM UW //#include "poszukiwania.h" #include "message.h" #include <climits> #include <cstdlib> #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #include <string> #include <string.h> #define pii pair<int,int> #define vi vector<int> #define f first #define s second #define x first #define y second #define Size(x) ((int)(x).size()) #define debon 1 #define deb(burak) if(debon) {cerr<<"DEB-> "<<#burak<<": "<<burak<<endl;} #define debv(burak) if(debon) {cerr<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cerr<<burak[zyx]<<" "; cerr<<endl;} #define debt(burak,SIzE) if(debon) {cerr<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cerr<<burak[zyx]<<" "; cerr<<endl;} #define debend if(debon) {cerr<<"_____________________"<<endl;} #define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));} using namespace std; typedef long long LL; int SeqLength() { return 2e2; } int SignalLength() { return 1e2; } int SeqAt(int i) { return (i - 1) % 7; } int SignalAt(int i) { return (i - 1) % 7; } vector<int> KMP(vector<int> &K, vector<int> &S) { vector<int> T(K.size() + 1, -1); vector<int> matches; if(K.size() == 0) { matches.push_back(0); return matches; } for(int i = 1; i <= K.size(); i++) { int pos = T[i - 1]; while(pos != -1 && K[pos] != K[i - 1]) pos = T[pos]; T[i] = pos + 1; } int sp = 0; int kp = 0; while(sp < S.size()) { while(kp != -1 && (kp == K.size() || K[kp] != S[sp])) kp = T[kp]; kp++; sp++; if(kp == K.size()) matches.push_back(sp - K.size()); } return matches; } const int MASTER = 0; int process_num, slaves_num; int pid; int sc_num, pc_num; // liczba string i pattern chunks int string_size, pattern_size; void init() { process_num = NumberOfNodes(); // todo obliczyc ile faktycznie bede uzywac slaves_num = process_num - 1; // chcemy zmaksymalizowac pc_num * sc_num, ale <= process_num pid = MyNodeId(); string_size = SeqLength(); pattern_size = SignalLength(); // TODO //slaves_num //pc_num = (int) sqrt( slaves_num * pattern_size / string_size ); //todo //sc_num = (int) sqrt( slaves_num * string_size / pattern_size ); //todo pc_num = sc_num = (int)sqrt(slaves_num ); slaves_num = pc_num * sc_num; deb(pc_num); deb(sc_num) } // ktory pattern chunk przypadal na p'ty proces int getPcNum(int p) { return (p - 1) / pc_num; } // analogicznie j.w. int getScNum(int p) { return (p - 1) % sc_num; } int getPatternChunkSize(int pc) { if (pc == pc_num - 1 && pattern_size % pc_num != 0) { return pattern_size % pc_num; } return pattern_size / pc_num; } int getPatternChunkStartPos(int pc) { return pc * getPatternChunkSize(0); } int getStringChunkSize(int sc) { int left_margin = (sc == 0) ? 0 : (getPatternChunkSize(0) - 1); int right_margin = (sc == sc_num - 1) ? 0 : (getPatternChunkSize(0) - 1); int inner_size = (sc == sc_num - 1 && string_size % sc_num != 0) ? string_size % sc_num : string_size / sc_num; return left_margin + inner_size + right_margin; } int getStringChunkStartPos(int sc) { int left_margin = (sc == 0) ? 0 : (getPatternChunkSize(0) - 1); int inner_size = string_size / sc_num; int res = sc * inner_size - left_margin; //int res = sc * getStringChunkSize(0) - left_margin; return res; } vector<int> getStringChunk(int sc) { int i = getStringChunkStartPos(sc); int s = getStringChunkSize(sc); if(pid == 4) { deb(i); deb(s); } vector<int> chunk; while(s--) { chunk.push_back( SeqAt(i) ); i++; } return chunk; } vector<int> getPatternChunk(int pc) { int i = getPatternChunkStartPos(pc); int s = getPatternChunkSize(pc); vector<int> chunk; while(s--) { chunk.push_back( SignalAt(i) ); i++; } return chunk; } int getOriginalPos(int sc, int pos_in_chunk) { return getStringChunkStartPos(sc) + pos_in_chunk; } // przesuwamy pozycja chunka do poczatkowego int scalePos(int pc, int pos_in_string) { return pos_in_string - getPatternChunkStartPos(pc); } int main() { init(); vector<pii>chunks; if (pid == MASTER) { for (int i = 1; i <= slaves_num; i++) { Receive(i); int n = GetInt(i); // ile znaleziono wystapien wzorca w i'tym procesie int pc = getPcNum(i); // ktory pattern chunk przypadal na i'ty proces int sc = getScNum(i); // ktory string chunk przypadal na i'ty proces while(n--) { int pos = GetInt(i); // pobieramy wystapienia od procesu chunks.push_back( pii(pos, pc) ); } } // sortujemy przeskalowane pozycje chunkow wraz z sort(chunks.begin(), chunks.end()); int chunk_counter = 0; int last_chunk_pos = -1; int res = 0; for (pii ch : chunks) { if (ch.first == last_chunk_pos) { chunk_counter++; } else { chunk_counter = 1; last_chunk_pos = ch.first; } if (chunk_counter == pc_num) { res++; } } printf("%d", res); } else { int pc = getPcNum(pid); // ktory pattern chunk int sc = getScNum(pid); // ktory string chunk vector<int> SC, PC, pos; SC = getStringChunk(sc); PC = getPatternChunk(pc); //deb(SC.size()); //deb(PC.size()); pos = KMP(PC, SC); //debv(SC); //debv(PC); deb(pos.size()); //debv(pos); for (auto &p : pos) { // przeskalowujemy wystapienie zgodnie z pozycją string chunka int org_pos = getOriginalPos(sc, p); int sca_pos = scalePos(pc, org_pos); // przesuwamy pozycja chunka do koncowego p = sca_pos; } // send PutInt(MASTER, pos.size()); for (auto p : pos) { PutInt(MASTER, p); } Send(MASTER); } 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 | //Michal Wos MIM UW //#include "poszukiwania.h" #include "message.h" #include <climits> #include <cstdlib> #include <iostream> #include <stdio.h> #include <vector> #include <algorithm> #include <set> #include <map> #include <cmath> #include <string> #include <string.h> #define pii pair<int,int> #define vi vector<int> #define f first #define s second #define x first #define y second #define Size(x) ((int)(x).size()) #define debon 1 #define deb(burak) if(debon) {cerr<<"DEB-> "<<#burak<<": "<<burak<<endl;} #define debv(burak) if(debon) {cerr<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cerr<<burak[zyx]<<" "; cerr<<endl;} #define debt(burak,SIzE) if(debon) {cerr<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cerr<<burak[zyx]<<" "; cerr<<endl;} #define debend if(debon) {cerr<<"_____________________"<<endl;} #define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));} using namespace std; typedef long long LL; int SeqLength() { return 2e2; } int SignalLength() { return 1e2; } int SeqAt(int i) { return (i - 1) % 7; } int SignalAt(int i) { return (i - 1) % 7; } vector<int> KMP(vector<int> &K, vector<int> &S) { vector<int> T(K.size() + 1, -1); vector<int> matches; if(K.size() == 0) { matches.push_back(0); return matches; } for(int i = 1; i <= K.size(); i++) { int pos = T[i - 1]; while(pos != -1 && K[pos] != K[i - 1]) pos = T[pos]; T[i] = pos + 1; } int sp = 0; int kp = 0; while(sp < S.size()) { while(kp != -1 && (kp == K.size() || K[kp] != S[sp])) kp = T[kp]; kp++; sp++; if(kp == K.size()) matches.push_back(sp - K.size()); } return matches; } const int MASTER = 0; int process_num, slaves_num; int pid; int sc_num, pc_num; // liczba string i pattern chunks int string_size, pattern_size; void init() { process_num = NumberOfNodes(); // todo obliczyc ile faktycznie bede uzywac slaves_num = process_num - 1; // chcemy zmaksymalizowac pc_num * sc_num, ale <= process_num pid = MyNodeId(); string_size = SeqLength(); pattern_size = SignalLength(); // TODO //slaves_num //pc_num = (int) sqrt( slaves_num * pattern_size / string_size ); //todo //sc_num = (int) sqrt( slaves_num * string_size / pattern_size ); //todo pc_num = sc_num = (int)sqrt(slaves_num ); slaves_num = pc_num * sc_num; deb(pc_num); deb(sc_num) } // ktory pattern chunk przypadal na p'ty proces int getPcNum(int p) { return (p - 1) / pc_num; } // analogicznie j.w. int getScNum(int p) { return (p - 1) % sc_num; } int getPatternChunkSize(int pc) { if (pc == pc_num - 1 && pattern_size % pc_num != 0) { return pattern_size % pc_num; } return pattern_size / pc_num; } int getPatternChunkStartPos(int pc) { return pc * getPatternChunkSize(0); } int getStringChunkSize(int sc) { int left_margin = (sc == 0) ? 0 : (getPatternChunkSize(0) - 1); int right_margin = (sc == sc_num - 1) ? 0 : (getPatternChunkSize(0) - 1); int inner_size = (sc == sc_num - 1 && string_size % sc_num != 0) ? string_size % sc_num : string_size / sc_num; return left_margin + inner_size + right_margin; } int getStringChunkStartPos(int sc) { int left_margin = (sc == 0) ? 0 : (getPatternChunkSize(0) - 1); int inner_size = string_size / sc_num; int res = sc * inner_size - left_margin; //int res = sc * getStringChunkSize(0) - left_margin; return res; } vector<int> getStringChunk(int sc) { int i = getStringChunkStartPos(sc); int s = getStringChunkSize(sc); if(pid == 4) { deb(i); deb(s); } vector<int> chunk; while(s--) { chunk.push_back( SeqAt(i) ); i++; } return chunk; } vector<int> getPatternChunk(int pc) { int i = getPatternChunkStartPos(pc); int s = getPatternChunkSize(pc); vector<int> chunk; while(s--) { chunk.push_back( SignalAt(i) ); i++; } return chunk; } int getOriginalPos(int sc, int pos_in_chunk) { return getStringChunkStartPos(sc) + pos_in_chunk; } // przesuwamy pozycja chunka do poczatkowego int scalePos(int pc, int pos_in_string) { return pos_in_string - getPatternChunkStartPos(pc); } int main() { init(); vector<pii>chunks; if (pid == MASTER) { for (int i = 1; i <= slaves_num; i++) { Receive(i); int n = GetInt(i); // ile znaleziono wystapien wzorca w i'tym procesie int pc = getPcNum(i); // ktory pattern chunk przypadal na i'ty proces int sc = getScNum(i); // ktory string chunk przypadal na i'ty proces while(n--) { int pos = GetInt(i); // pobieramy wystapienia od procesu chunks.push_back( pii(pos, pc) ); } } // sortujemy przeskalowane pozycje chunkow wraz z sort(chunks.begin(), chunks.end()); int chunk_counter = 0; int last_chunk_pos = -1; int res = 0; for (pii ch : chunks) { if (ch.first == last_chunk_pos) { chunk_counter++; } else { chunk_counter = 1; last_chunk_pos = ch.first; } if (chunk_counter == pc_num) { res++; } } printf("%d", res); } else { int pc = getPcNum(pid); // ktory pattern chunk int sc = getScNum(pid); // ktory string chunk vector<int> SC, PC, pos; SC = getStringChunk(sc); PC = getPatternChunk(pc); //deb(SC.size()); //deb(PC.size()); pos = KMP(PC, SC); //debv(SC); //debv(PC); deb(pos.size()); //debv(pos); for (auto &p : pos) { // przeskalowujemy wystapienie zgodnie z pozycją string chunka int org_pos = getOriginalPos(sc, p); int sca_pos = scalePos(pc, org_pos); // przesuwamy pozycja chunka do koncowego p = sca_pos; } // send PutInt(MASTER, pos.size()); for (auto p : pos) { PutInt(MASTER, p); } Send(MASTER); } return 0; } |