#include "message.h"
#include <cstdio>
#include <vector>
using namespace std;
char slowo1[ 100005 ], slowo2[ 100005 ];
int n, m, length, level = 1, NODES;
pair <int,int> tabela[ 100005 ][ 2 ];
inline pair <int,int> mini( pair <int,int> a, pair <int,int> b ){
if (a.first < b.first){
return a;
}
else if ((a.first == b.first) && (a.second < b.second)){
return a;
}
else {
return b;
}
}
int main(){
scanf("%d%d",&n,&m);
scanf("%s", slowo1);
scanf("%s", slowo2);
NODES = NumberOfNodes();
if (m <= 101){
NODES = 1;
}
length = (m-1) / NODES;
int p = length * MyNodeId(), k = length * (MyNodeId() + 1) - 1;
if (MyNodeId() == NODES-1){
k = m-1;
}
for (int i = p; i <= k+1; i++){
tabela[ i-p ][ 0 ] = make_pair(i,0);
}
if ((MyNodeId() > 0) && (MyNodeId() < NODES)){
while (true){
Receive( MyNodeId()-1 );
int s = GetInt( MyNodeId() - 1 );
if (MyNodeId() < NODES-1){
PutInt( MyNodeId()+1, s );
}
for (int i = 0; i < s; i++){
int a = GetInt( MyNodeId() - 1 ), b = GetInt( MyNodeId() - 1 );
tabela[ 0 ][ 1 ] = make_pair( a, b );
for (int j = p; j <= k; j++){
if ( slowo1[ level-1 ] == slowo2[ j ]){
pair <int,int> a = tabela[ j-p ][ 1 ];
a.first++;
pair <int,int> b = tabela[ j-p+1 ][ 0 ];
b.first++;
pair <int,int> c = tabela[ j-p ][ 0 ];
tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c );
}
else if (slowo1[ level-1 ] < slowo2[ j ]){
pair <int,int> a = tabela[ j-p ][ 1 ];
a.first++;
pair <int,int> b = tabela[ j-p+1 ][ 0 ];
b.first++;
pair <int,int> c = tabela[ j-p ][ 0 ];
c.first++;
c.second++;
tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c );
}
else {
pair <int,int> a = tabela[ j-p ][ 1 ];
a.first++;
pair <int,int> b = tabela[ j-p+1 ][ 0 ];
b.first++;
pair <int,int> c = tabela[ j-p ][ 0 ];
c.first++;
tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c );
}
}
if (MyNodeId() < NODES-1){
PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].first );
PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].second );
}
for (int j = p; j <= k+1; j++){
tabela[ j-p ][ 0 ] = tabela[ j-p ][ 1 ];
tabela[ j-p ][ 1 ] = make_pair( 0,0 );
}
level++;
}
if (MyNodeId() < NODES-1)
Send( MyNodeId()+1 );
if (level == n+1){
break;
}
}
if (MyNodeId() == NODES-1){
printf("%d %d\n", tabela[k-p+1][ 0 ].first, tabela[k-p+1][ 0 ].second);
}
}
else if (MyNodeId() < NODES){
int siz = max( 1, n / 800 );
while (true){
if ((level-1) % siz == 0){
if ((level-1+siz <= n) && (MyNodeId() < NODES-1)){
PutInt(MyNodeId()+1, siz);
}
else if (MyNodeId() < NODES-1){
PutInt(MyNodeId()+1, n - (level-1));
}
}
tabela[ 0 ][ 1 ] = make_pair(level,0);
for (int j = p; j <= k; j++){
if ( slowo1[ level-1 ] == slowo2[ j ]){
pair <int,int> a = tabela[ j-p ][ 1 ];
a.first++;
pair <int,int> b = tabela[ j-p+1 ][ 0 ];
b.first++;
pair <int,int> c = tabela[ j-p ][ 0 ];
tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c );
}
else if (slowo1[ level-1 ] < slowo2[ j ]){
pair <int,int> a = tabela[ j-p ][ 1 ];
a.first++;
pair <int,int> b = tabela[ j-p+1 ][ 0 ];
b.first++;
pair <int,int> c = tabela[ j-p ][ 0 ];
c.first++;
c.second++;
tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c );
}
else {
pair <int,int> a = tabela[ j-p ][ 1 ];
a.first++;
pair <int,int> b = tabela[ j-p+1 ][ 0 ];
b.first++;
pair <int,int> c = tabela[ j-p ][ 0 ];
c.first++;
tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c );
}
}
if (MyNodeId() < NODES-1){
PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].first );
PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].second );
}
for (int j = p; j <= k+1; j++){
tabela[ j-p ][ 0 ] = tabela[ j-p ][ 1 ];
tabela[ j-p ][ 1 ] = make_pair( 0,0 );
}
if ((level % siz == 0) || (level == n)){
if (MyNodeId() < NODES-1){
Send(MyNodeId()+1);
}
}
level++;
if (level == n+1)
break;
}
if (MyNodeId() == NODES-1){
printf("%d %d\n", tabela[k-p+1][ 0 ].first, tabela[k-p+1][ 0 ].second);
}
}
}
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 | #include "message.h" #include <cstdio> #include <vector> using namespace std; char slowo1[ 100005 ], slowo2[ 100005 ]; int n, m, length, level = 1, NODES; pair <int,int> tabela[ 100005 ][ 2 ]; inline pair <int,int> mini( pair <int,int> a, pair <int,int> b ){ if (a.first < b.first){ return a; } else if ((a.first == b.first) && (a.second < b.second)){ return a; } else { return b; } } int main(){ scanf("%d%d",&n,&m); scanf("%s", slowo1); scanf("%s", slowo2); NODES = NumberOfNodes(); if (m <= 101){ NODES = 1; } length = (m-1) / NODES; int p = length * MyNodeId(), k = length * (MyNodeId() + 1) - 1; if (MyNodeId() == NODES-1){ k = m-1; } for (int i = p; i <= k+1; i++){ tabela[ i-p ][ 0 ] = make_pair(i,0); } if ((MyNodeId() > 0) && (MyNodeId() < NODES)){ while (true){ Receive( MyNodeId()-1 ); int s = GetInt( MyNodeId() - 1 ); if (MyNodeId() < NODES-1){ PutInt( MyNodeId()+1, s ); } for (int i = 0; i < s; i++){ int a = GetInt( MyNodeId() - 1 ), b = GetInt( MyNodeId() - 1 ); tabela[ 0 ][ 1 ] = make_pair( a, b ); for (int j = p; j <= k; j++){ if ( slowo1[ level-1 ] == slowo2[ j ]){ pair <int,int> a = tabela[ j-p ][ 1 ]; a.first++; pair <int,int> b = tabela[ j-p+1 ][ 0 ]; b.first++; pair <int,int> c = tabela[ j-p ][ 0 ]; tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c ); } else if (slowo1[ level-1 ] < slowo2[ j ]){ pair <int,int> a = tabela[ j-p ][ 1 ]; a.first++; pair <int,int> b = tabela[ j-p+1 ][ 0 ]; b.first++; pair <int,int> c = tabela[ j-p ][ 0 ]; c.first++; c.second++; tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c ); } else { pair <int,int> a = tabela[ j-p ][ 1 ]; a.first++; pair <int,int> b = tabela[ j-p+1 ][ 0 ]; b.first++; pair <int,int> c = tabela[ j-p ][ 0 ]; c.first++; tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c ); } } if (MyNodeId() < NODES-1){ PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].first ); PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].second ); } for (int j = p; j <= k+1; j++){ tabela[ j-p ][ 0 ] = tabela[ j-p ][ 1 ]; tabela[ j-p ][ 1 ] = make_pair( 0,0 ); } level++; } if (MyNodeId() < NODES-1) Send( MyNodeId()+1 ); if (level == n+1){ break; } } if (MyNodeId() == NODES-1){ printf("%d %d\n", tabela[k-p+1][ 0 ].first, tabela[k-p+1][ 0 ].second); } } else if (MyNodeId() < NODES){ int siz = max( 1, n / 800 ); while (true){ if ((level-1) % siz == 0){ if ((level-1+siz <= n) && (MyNodeId() < NODES-1)){ PutInt(MyNodeId()+1, siz); } else if (MyNodeId() < NODES-1){ PutInt(MyNodeId()+1, n - (level-1)); } } tabela[ 0 ][ 1 ] = make_pair(level,0); for (int j = p; j <= k; j++){ if ( slowo1[ level-1 ] == slowo2[ j ]){ pair <int,int> a = tabela[ j-p ][ 1 ]; a.first++; pair <int,int> b = tabela[ j-p+1 ][ 0 ]; b.first++; pair <int,int> c = tabela[ j-p ][ 0 ]; tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c ); } else if (slowo1[ level-1 ] < slowo2[ j ]){ pair <int,int> a = tabela[ j-p ][ 1 ]; a.first++; pair <int,int> b = tabela[ j-p+1 ][ 0 ]; b.first++; pair <int,int> c = tabela[ j-p ][ 0 ]; c.first++; c.second++; tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c ); } else { pair <int,int> a = tabela[ j-p ][ 1 ]; a.first++; pair <int,int> b = tabela[ j-p+1 ][ 0 ]; b.first++; pair <int,int> c = tabela[ j-p ][ 0 ]; c.first++; tabela[ j-p+1 ][ 1 ] = mini( mini( a, b ), c ); } } if (MyNodeId() < NODES-1){ PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].first ); PutInt( MyNodeId()+1 , tabela[ k-p+1 ][ 1 ].second ); } for (int j = p; j <= k+1; j++){ tabela[ j-p ][ 0 ] = tabela[ j-p ][ 1 ]; tabela[ j-p ][ 1 ] = make_pair( 0,0 ); } if ((level % siz == 0) || (level == n)){ if (MyNodeId() < NODES-1){ Send(MyNodeId()+1); } } level++; if (level == n+1) break; } if (MyNodeId() == NODES-1){ printf("%d %d\n", tabela[k-p+1][ 0 ].first, tabela[k-p+1][ 0 ].second); } } } |
English