#include "message.h" #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<map> using namespace std; typedef pair<int,int> PI; typedef long long LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define MAX 100101 int n,m,proc,id; char s[MAX],s2[MAX]; PI dp[2][MAX]; const int C = 128; inline void mini(PI &a,PI b,PI c,PI d,int cz){ b.FI++; c.FI++; d.FI++; d.SE+=cz; a = b; if(a>c)a=c; if(a>d)a=d; } void licz(int po,int ko){ po = min(m,po);ko = min(m,ko); if(po == ko)exit(0); R(i,n)F(j,po,ko+1)dp[0][j] = MP(j,0); int ii = 1; R(i,n){ if(id == 0){ dp[ii][po] = MP(i+1,0); }else{ if((i&(C-1)) == 0)Receive(id-1); dp[ii][po].FI = GetInt(id-1); dp[ii][po].SE = GetInt(id-1); } F(j,po,ko){ if(s[i] == s2[j]) dp[ii][j+1] = dp[!ii][j]; else mini(dp[ii][j+1],dp[ii][j],dp[!ii][j+1],dp[!ii][j],(s[i] < s2[j])); } if(id != proc-1){ PutInt(id+1,dp[ii][ko].FI); PutInt(id+1,dp[ii][ko].SE); if((i&(C-1))==C-1 || i==n-1) Send(id+1); } ii=!ii; } if(ko == m){ printf("%d %d\n",dp[!ii][m].FI,dp[!ii][m].SE); } } main(){ proc = NumberOfNodes(); id = MyNodeId(); make(n);make(m); scanf(" %s %s",s,s2); int ile = (m+proc-1)/proc; licz(id * ile,(id+1)*ile); }
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 | #include "message.h" #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<queue> #include<stack> #include<cmath> #include<map> using namespace std; typedef pair<int,int> PI; typedef long long LL; typedef double D; #define FI first #define SE second #define MP make_pair #define PB push_back #define R(I,N) for(int I=0;I<N;I++) #define F(I,A,B) for(int I=A;I<B;I++) #define FD(I,N) for(int I=N-1;I>=0;I--) #define make(A) scanf("%d",&A) #define MAX 100101 int n,m,proc,id; char s[MAX],s2[MAX]; PI dp[2][MAX]; const int C = 128; inline void mini(PI &a,PI b,PI c,PI d,int cz){ b.FI++; c.FI++; d.FI++; d.SE+=cz; a = b; if(a>c)a=c; if(a>d)a=d; } void licz(int po,int ko){ po = min(m,po);ko = min(m,ko); if(po == ko)exit(0); R(i,n)F(j,po,ko+1)dp[0][j] = MP(j,0); int ii = 1; R(i,n){ if(id == 0){ dp[ii][po] = MP(i+1,0); }else{ if((i&(C-1)) == 0)Receive(id-1); dp[ii][po].FI = GetInt(id-1); dp[ii][po].SE = GetInt(id-1); } F(j,po,ko){ if(s[i] == s2[j]) dp[ii][j+1] = dp[!ii][j]; else mini(dp[ii][j+1],dp[ii][j],dp[!ii][j+1],dp[!ii][j],(s[i] < s2[j])); } if(id != proc-1){ PutInt(id+1,dp[ii][ko].FI); PutInt(id+1,dp[ii][ko].SE); if((i&(C-1))==C-1 || i==n-1) Send(id+1); } ii=!ii; } if(ko == m){ printf("%d %d\n",dp[!ii][m].FI,dp[!ii][m].SE); } } main(){ proc = NumberOfNodes(); id = MyNodeId(); make(n);make(m); scanf(" %s %s",s,s2); int ile = (m+proc-1)/proc; licz(id * ile,(id+1)*ile); } |