#include <iostream> #include <vector> #include <algorithm> std::vector<std::string> dryb; long long pocz[600][605][2]; long long kon[600][605][2]; long state[2][605][2]; long long results[601]; int mod=1000*1000*1000+7; void ocen(int dr){ for(int i=0;i<=dryb[dr].size()+1;i++){ state[0][i][0]=0; state[0][i][1]=0; state[1][i][0]=0; state[1][i][1]=0; } state[0][0][0]=1; char last='L'; results[dr]=0; bool ne=1; for(int i=0;i<dryb[dr].size();i++){ ne=(i%2==0); bool ol=(i%2); char l=dryb[dr][i]; //std::cout<<"l:"<<l<<std::endl; if(l=='L' && last=='L') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][0]+state[ol][0][1]; for (int j = 1; j <= i+1; j++) { //std::cout<<"x"<<state[ol][j - 1][0]<<std::endl; state[ne][j][0] = state[ol][j - 1][0]; state[ne][j][1] = state[ol][j][0]+state[ol][j][1]; if(state[ne][j][1]>=mod) state[ne][j][1]-=mod; } } if(l=='L' && last=='P') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][0]; for (int j = 1; j <= i+1; j++) { state[ne][j][0] = state[ol][j - 1][0]+state[ol][j - 1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } if(l=='P' && last=='P') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j+1][0]; state[ne][j][1] = state[ol][j][0]+state[ol][j][1]; if(state[ne][j][1]>=mod) state[ne][j][1]-=mod; } } if(l=='P' && last=='L') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j + 1][0]+state[ol][j + 1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } last=l; results[dr]+=state[ne][0][0]; results[dr]%=mod; //for(int i=0;i<=dryb[dr].size();i++){ //std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; //} //std::cout<<std::endl; } for(int i=0;i<=dryb[dr].size();i++){ //std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; pocz[dr][i][0]=state[ne][i][0]; pocz[dr][i][1]=state[ne][i][1]; } //std::cout<<"res"<<dr<<" "<<results[dr]<<std::endl; } void ocen2(int dr){ for(int i=0;i<=dryb[dr].size()+1;i++){ state[0][i][0]=0; state[0][i][1]=0; state[1][i][0]=0; state[1][i][1]=0; } state[0][0][0]=0; char last='P'; bool ne=1; for(int i=0;i<dryb[dr].size();i++){ ne=(i%2==0); bool ol=(i%2); char l=dryb[dr][dryb[dr].size()-1-i]; //std::cout<<"l:"<<l<<std::endl; if(l=='L' && last=='L') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j + 1][0]+state[ol][j+1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][1]; } } if(l=='L' && last=='P') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j + 1][0]+state[ol][j + 1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } //DONE modify below; if(l=='P' && last=='P') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][1]; for (int j = 1; j <= i+1; j++) { state[ne][j][0] = state[ol][j-1][0]+state[ol][j-1][1]+(j==1); if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][1]; } } if(l=='P' && last=='L') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][0]; for (int j = 1; j <= i+1; j++) { state[ne][j][0] = state[ol][j - 1][0]+state[ol][j - 1][1]+(j==1);; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } last=l; //for(int i=0;i<=dryb[dr].size();i++){ // std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; //} //std::cout<<std::endl; } for(int i=0;i<=dryb[dr].size();i++){ //std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; kon[dr][i][0]=state[ne][i][0]; kon[dr][i][1]=state[ne][i][1]; } //std::cout<<"res"<<dr<<" "<<results[dr]<<std::endl; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::string text; int n; std::cin >> n; for(int i=0;i<n;i++){ std::cin>>text; dryb.emplace_back(text); } for(int i=0;i<n;i++){ ocen(i); ocen2(i); } for(int i=0;i<n;i++) { for (int j = 0; j < n; j++) { int dl=std::min(dryb[i].size(),dryb[j].size())+1; char la=dryb[i].back(); char fi=dryb[j][0]; long long res=results[i]; for(int k=0;k<=dl;k++){ res+=(pocz[i][k][0]*(kon[j][k][0]+kon[j][k][1])); if(fi==la) res+=pocz[i][k][1]*kon[j][k][1]; else res+=pocz[i][k][1]*kon[j][k][0]; res%=mod; //std::cout<<res<<std::endl; } std::cout<<res<<" "; } std::cout<<"\n"; } }
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 | #include <iostream> #include <vector> #include <algorithm> std::vector<std::string> dryb; long long pocz[600][605][2]; long long kon[600][605][2]; long state[2][605][2]; long long results[601]; int mod=1000*1000*1000+7; void ocen(int dr){ for(int i=0;i<=dryb[dr].size()+1;i++){ state[0][i][0]=0; state[0][i][1]=0; state[1][i][0]=0; state[1][i][1]=0; } state[0][0][0]=1; char last='L'; results[dr]=0; bool ne=1; for(int i=0;i<dryb[dr].size();i++){ ne=(i%2==0); bool ol=(i%2); char l=dryb[dr][i]; //std::cout<<"l:"<<l<<std::endl; if(l=='L' && last=='L') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][0]+state[ol][0][1]; for (int j = 1; j <= i+1; j++) { //std::cout<<"x"<<state[ol][j - 1][0]<<std::endl; state[ne][j][0] = state[ol][j - 1][0]; state[ne][j][1] = state[ol][j][0]+state[ol][j][1]; if(state[ne][j][1]>=mod) state[ne][j][1]-=mod; } } if(l=='L' && last=='P') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][0]; for (int j = 1; j <= i+1; j++) { state[ne][j][0] = state[ol][j - 1][0]+state[ol][j - 1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } if(l=='P' && last=='P') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j+1][0]; state[ne][j][1] = state[ol][j][0]+state[ol][j][1]; if(state[ne][j][1]>=mod) state[ne][j][1]-=mod; } } if(l=='P' && last=='L') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j + 1][0]+state[ol][j + 1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } last=l; results[dr]+=state[ne][0][0]; results[dr]%=mod; //for(int i=0;i<=dryb[dr].size();i++){ //std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; //} //std::cout<<std::endl; } for(int i=0;i<=dryb[dr].size();i++){ //std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; pocz[dr][i][0]=state[ne][i][0]; pocz[dr][i][1]=state[ne][i][1]; } //std::cout<<"res"<<dr<<" "<<results[dr]<<std::endl; } void ocen2(int dr){ for(int i=0;i<=dryb[dr].size()+1;i++){ state[0][i][0]=0; state[0][i][1]=0; state[1][i][0]=0; state[1][i][1]=0; } state[0][0][0]=0; char last='P'; bool ne=1; for(int i=0;i<dryb[dr].size();i++){ ne=(i%2==0); bool ol=(i%2); char l=dryb[dr][dryb[dr].size()-1-i]; //std::cout<<"l:"<<l<<std::endl; if(l=='L' && last=='L') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j + 1][0]+state[ol][j+1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][1]; } } if(l=='L' && last=='P') { for (int j = 0; j <= i+1; j++) { state[ne][j][0] = state[ol][j + 1][0]+state[ol][j + 1][1]; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } //DONE modify below; if(l=='P' && last=='P') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][1]; for (int j = 1; j <= i+1; j++) { state[ne][j][0] = state[ol][j-1][0]+state[ol][j-1][1]+(j==1); if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][1]; } } if(l=='P' && last=='L') { state[ne][0][0]=0; state[ne][0][1] = state[ol][0][0]; for (int j = 1; j <= i+1; j++) { state[ne][j][0] = state[ol][j - 1][0]+state[ol][j - 1][1]+(j==1);; if(state[ne][j][0]>=mod) state[ne][j][0]-=mod; state[ne][j][1] = state[ol][j][0]; } } last=l; //for(int i=0;i<=dryb[dr].size();i++){ // std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; //} //std::cout<<std::endl; } for(int i=0;i<=dryb[dr].size();i++){ //std::cout<<state[ne][i][0]<<" "<<state[ne][i][1]<<std::endl; kon[dr][i][0]=state[ne][i][0]; kon[dr][i][1]=state[ne][i][1]; } //std::cout<<"res"<<dr<<" "<<results[dr]<<std::endl; } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); std::string text; int n; std::cin >> n; for(int i=0;i<n;i++){ std::cin>>text; dryb.emplace_back(text); } for(int i=0;i<n;i++){ ocen(i); ocen2(i); } for(int i=0;i<n;i++) { for (int j = 0; j < n; j++) { int dl=std::min(dryb[i].size(),dryb[j].size())+1; char la=dryb[i].back(); char fi=dryb[j][0]; long long res=results[i]; for(int k=0;k<=dl;k++){ res+=(pocz[i][k][0]*(kon[j][k][0]+kon[j][k][1])); if(fi==la) res+=pocz[i][k][1]*kon[j][k][1]; else res+=pocz[i][k][1]*kon[j][k][0]; res%=mod; //std::cout<<res<<std::endl; } std::cout<<res<<" "; } std::cout<<"\n"; } } |