#include <unordered_map> #include <iostream> using namespace std; constexpr long MOD = 1e9 + 7; bool balanced(string str) { int l = 0, p = 0; for (int i = 0; i < (int)str.size(); i++) { if (str[i] == 'L') l++; else p++; if (p > l) return false; } return l == p && l != 0; } void fill_subseq(unordered_map<string,bool>& M, string origin, string substr = "") { if (origin.empty()) { M[substr] = true; return; } fill_subseq(M, origin.substr(1), substr); fill_subseq(M, origin.substr(1), substr + origin.front()); } long count_substr(string str) { long result = 0; unordered_map<string,bool> visited; fill_subseq(visited, str); for (auto p : visited) { result += balanced(p.first); } return result % MOD; } const int N = 600 + 1; string t[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << count_substr(t[i] + t[j]) << ' '; } 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 | #include <unordered_map> #include <iostream> using namespace std; constexpr long MOD = 1e9 + 7; bool balanced(string str) { int l = 0, p = 0; for (int i = 0; i < (int)str.size(); i++) { if (str[i] == 'L') l++; else p++; if (p > l) return false; } return l == p && l != 0; } void fill_subseq(unordered_map<string,bool>& M, string origin, string substr = "") { if (origin.empty()) { M[substr] = true; return; } fill_subseq(M, origin.substr(1), substr); fill_subseq(M, origin.substr(1), substr + origin.front()); } long count_substr(string str) { long result = 0; unordered_map<string,bool> visited; fill_subseq(visited, str); for (auto p : visited) { result += balanced(p.first); } return result % MOD; } const int N = 600 + 1; string t[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> t[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << count_substr(t[i] + t[j]) << ' '; } cout << '\n'; } } |