#include <iostream> #include <string> using namespace std; int max_size = 600; unsigned int *mapL = new unsigned int[max_size * 2 + 1]; unsigned int *mapP = new unsigned int[max_size * 2 + 1]; unsigned int *total = new unsigned int[max_size * 2 + 1]; int modulo = 1000000007; int count(string sequence) { //string sequence = first; fill_n(total, sequence.size() + 1, 0); fill_n(mapL, sequence.size() + 1, 0); fill_n(mapP, sequence.size() + 1, 0); int levelCount; int level = 1; total[0] = 1; mapL[0] = 1; int lastLevel = 1; for (int i = 0; i < sequence.size(); i++) { // cout << "---------------------------------\n"; // cout << "Letter: " << sequence[i] << "\n"; // cout << "Index: " << i << "\n"; switch (sequence[i]) { case 'L': level = lastLevel; levelCount = total[level - 1]; total[level] = (total[level] + levelCount)% modulo; mapL[level] = levelCount; level--; while (level >= 1) { levelCount = total[level - 1]; total[level] = (levelCount - mapL[level] + total[level])% modulo; mapL[level] = levelCount; level--; } lastLevel++; break; case 'P': level = 0; while (total[level + 1] > 0) { levelCount = total[level + 1]; total[level] = (total[level] + levelCount - mapP[level])% modulo; mapP[level] = levelCount; level++; } break; } // cout << "total: " << total[0] << ", " << total[1] << ", " << total[2] << ", " << total[3] << ", " << total[4] << ", " << total[5] << "\n"; // cout << "map L: " << mapL[0] << ", " << mapL[1] << ", " << mapL[2] << ", " << mapL[3] << ", " << mapL[4] << ", " << mapL[5] << ", " << mapL[6] << ", " << mapL[7] << ", "<< "\n"; // cout << "map P: " << mapP[0] << ", " << mapP[1] << ", " << mapP[2] << ", " << mapP[3] << ", " << mapP[4] << ", " << mapP[5] << ", " << mapP[6] << ", " << mapP[7] << ", "<< "\n"; // cout << "level: " << level << "\n"; } return total[0] - 1; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; string sequences[600]; //string test = "LLLLPPP"; cin >> n; //cin >> test; //cout << count(test); for(int i = 0; i < n; i++) { cin >> sequences[i]; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cout << count(sequences[i] + sequences[j]); cout << ((j == n - 1) ? "\n" : " "); } } 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 | #include <iostream> #include <string> using namespace std; int max_size = 600; unsigned int *mapL = new unsigned int[max_size * 2 + 1]; unsigned int *mapP = new unsigned int[max_size * 2 + 1]; unsigned int *total = new unsigned int[max_size * 2 + 1]; int modulo = 1000000007; int count(string sequence) { //string sequence = first; fill_n(total, sequence.size() + 1, 0); fill_n(mapL, sequence.size() + 1, 0); fill_n(mapP, sequence.size() + 1, 0); int levelCount; int level = 1; total[0] = 1; mapL[0] = 1; int lastLevel = 1; for (int i = 0; i < sequence.size(); i++) { // cout << "---------------------------------\n"; // cout << "Letter: " << sequence[i] << "\n"; // cout << "Index: " << i << "\n"; switch (sequence[i]) { case 'L': level = lastLevel; levelCount = total[level - 1]; total[level] = (total[level] + levelCount)% modulo; mapL[level] = levelCount; level--; while (level >= 1) { levelCount = total[level - 1]; total[level] = (levelCount - mapL[level] + total[level])% modulo; mapL[level] = levelCount; level--; } lastLevel++; break; case 'P': level = 0; while (total[level + 1] > 0) { levelCount = total[level + 1]; total[level] = (total[level] + levelCount - mapP[level])% modulo; mapP[level] = levelCount; level++; } break; } // cout << "total: " << total[0] << ", " << total[1] << ", " << total[2] << ", " << total[3] << ", " << total[4] << ", " << total[5] << "\n"; // cout << "map L: " << mapL[0] << ", " << mapL[1] << ", " << mapL[2] << ", " << mapL[3] << ", " << mapL[4] << ", " << mapL[5] << ", " << mapL[6] << ", " << mapL[7] << ", "<< "\n"; // cout << "map P: " << mapP[0] << ", " << mapP[1] << ", " << mapP[2] << ", " << mapP[3] << ", " << mapP[4] << ", " << mapP[5] << ", " << mapP[6] << ", " << mapP[7] << ", "<< "\n"; // cout << "level: " << level << "\n"; } return total[0] - 1; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; string sequences[600]; //string test = "LLLLPPP"; cin >> n; //cin >> test; //cout << count(test); for(int i = 0; i < n; i++) { cin >> sequences[i]; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { cout << count(sequences[i] + sequences[j]); cout << ((j == n - 1) ? "\n" : " "); } } return 0; } |