#include "palindromy.h"
#include "message.h"
#include <iostream>
using namespace std;
bool is_palindrom(char * slowo, long long int len){
long long int start = 0;
long long int end = len - 1;
while(slowo[start] == slowo[end]){
start += 1;
end -= 1;
if (start > end)
return true;
}
return false;
}
int main() {
int node_nums = NumberOfNodes();
int id = MyNodeId();
long long int len = GetLength();
long long int sum = 0;
char * slowo = new char[len];
for (long long int i=0; i<len; i++){
slowo[i] = GetLetter(i);
}
// distribute work and count
for (long long int k = (id + 2); k <= len; k += node_nums){
long long int sub_len = k;
for(long long int i = 0; i+sub_len <= len; i++){
if(is_palindrom(&slowo[i], sub_len)){
sum += 1;
}
}
}
// koniec procedury
delete[] slowo;
int target;
int source;
/*
// zbieranie wyników ze wszystkich węzłów
int half;
int active_nodes = node_nums;
while(active_nodes > 1){
if (id < active_nodes){
if ((active_nodes % 2) == 0){
half = active_nodes/2;
if (id >= half){
target = id-half;
PutLL(target, sum);
Send(target);
}
else{
source = id+half;
Receive(source);
sum += GetLL(source);
}
active_nodes /= 2;
}
else{
// nieparzysta liczba węzłów - ostatni wysyla do przedostatniego
if (id == (active_nodes-1)){
// wyslanie wynikow do węzłą id - 1
target = id-1;
PutLL(target, sum);
Send(target);
}
if (id == (active_nodes-2)){
// odebranie wyniku z węzła id + 1
source = id+1;
Receive(source);
sum += GetLL(source);
}
active_nodes -= 1;
}
}
}
*/
if (id >0){
PutLL(0, sum);
Send(0);
}
else{
for(int n =1; n < node_nums; n++){
Receive(n);
sum += GetLL(n);
}
}
// ostatni krok - drukuj wynik
if (id == 0){
sum += len;
cout << sum << endl;
}
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 | #include "palindromy.h" #include "message.h" #include <iostream> using namespace std; bool is_palindrom(char * slowo, long long int len){ long long int start = 0; long long int end = len - 1; while(slowo[start] == slowo[end]){ start += 1; end -= 1; if (start > end) return true; } return false; } int main() { int node_nums = NumberOfNodes(); int id = MyNodeId(); long long int len = GetLength(); long long int sum = 0; char * slowo = new char[len]; for (long long int i=0; i<len; i++){ slowo[i] = GetLetter(i); } // distribute work and count for (long long int k = (id + 2); k <= len; k += node_nums){ long long int sub_len = k; for(long long int i = 0; i+sub_len <= len; i++){ if(is_palindrom(&slowo[i], sub_len)){ sum += 1; } } } // koniec procedury delete[] slowo; int target; int source; /* // zbieranie wyników ze wszystkich węzłów int half; int active_nodes = node_nums; while(active_nodes > 1){ if (id < active_nodes){ if ((active_nodes % 2) == 0){ half = active_nodes/2; if (id >= half){ target = id-half; PutLL(target, sum); Send(target); } else{ source = id+half; Receive(source); sum += GetLL(source); } active_nodes /= 2; } else{ // nieparzysta liczba węzłów - ostatni wysyla do przedostatniego if (id == (active_nodes-1)){ // wyslanie wynikow do węzłą id - 1 target = id-1; PutLL(target, sum); Send(target); } if (id == (active_nodes-2)){ // odebranie wyniku z węzła id + 1 source = id+1; Receive(source); sum += GetLL(source); } active_nodes -= 1; } } } */ if (id >0){ PutLL(0, sum); Send(0); } else{ for(int n =1; n < node_nums; n++){ Receive(n); sum += GetLL(n); } } // ostatni krok - drukuj wynik if (id == 0){ sum += len; cout << sum << endl; } return 0; } |
English